in reply to Re: Yet another Algorithm::Diff question
in thread Yet another Algorithm::Diff question

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.

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

Replies are listed 'Best First'.
Re: Re: Re: Yet another Algorithm::Diff question
by mdillon (Priest) on Dec 05, 2000 at 11:44 UTC
    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";