Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Yet another Algorithm::Diff question

by chipmunk (Parson)
on Dec 05, 2000 at 10:14 UTC ( [id://44956]=note: print w/replies, xml ) Need Help??


in reply to Yet another Algorithm::Diff question

The 'n' is at line 9 instead of line 8 because at that point you've removed 2 elements and added 3, shifting everything after that up by one.

Solution: apply your changes starting at the end of the array, and work towards the beginning.

  • Comment on Re: Yet another Algorithm::Diff question

Replies are listed 'Best First'.
Re: Re: Yet another Algorithm::Diff question
by extremely (Priest) on Dec 05, 2000 at 10:18 UTC
    Exactly, or keep a line offset counter and work forward!

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: Re: Yet another Algorithm::Diff question
by chipmunk (Parson) on Dec 05, 2000 at 10:25 UTC
    Oh, that won't work either... It looks like the - instructions are indexes into the original array, while the + instructions are indexes into the new array... Try this:

    First, process all the delete instructions, starting at the end. Then, process all the insert instructions, starting at the beginning.

    I tested that by hand and it came out properly.

      here's my code for it:
      #!/usr/bin/perl -w use strict; use Data::Dumper; use Algorithm::Diff qw(diff); my @orig = qw(a b c e h j l m n p); my @rev = qw(b c d e f j k l m r s t); print "Original:\t@orig\n"; print "Revision:\t@rev\n"; my $diff = diff \@orig, \@rev; my @adds; for my $hunk (reverse @$diff) { for my $change (reverse @$hunk) { if($change->[0] eq "-") { # process deletions splice @orig, $change->[1], 1; } elsif ($change->[0] eq "+") { # defer handling additions unshift @adds, $change; } } } # process additions splice @orig, $_->[1], 0, $_->[2] for @adds; print "Patched:\t@orig\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://44956]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-19 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found