I'm sure (or hopefully) I'm 90% there but the last 10% is stretching out longer than expected.
I'm taking the results of a source compare using Algorithm::Diff and am trying to use the "$diffs" to highlight the changes in the compared files just like you would see in a graphical diff tool like KDE Kompare or any other. I need to perform some other processing which lead me to implementing a solution.
I've tried many ways and always get close but no cigar.
Any help will be greatly appreciated.
Here is my current iteration which is supposed to display the changed file with highlighting.
#!/usr/bin/perl # use Algorithm::Diff qw(diff); my $file1 = '/file1.txt'; my $file2 = '/file2.txt'; -T $file1 or bag("$file1: binary"); -T $file2 or bag("$file2: binary"); open (F1, $file1) or bag("Couldn't open $file1: $!"); open (F2, $file2) or bag("Couldn't open $file2: $!"); chomp(@f1 = <F1>); close F1; chomp(@f2 = <F2>); close F2; $diffs = diff(\@f1, \@f2); exit 0 unless @$diffs; my @f11; my $lineno = 0; foreach (@f1) { push @f11, [' ', $lineno, $_]; $lineno++; } my @f22; $lineno = 0; foreach (@f2) { push @f22, [' ', $lineno, $_]; $lineno++; } foreach $chunk (@$diffs) { foreach $line (@$chunk) { my $i = 0; my ($sign, $lineno, $text) = @$line; foreach (@f11) { if ($_->[1] eq $lineno) { if ($sign eq '+') { splice @f11, $i, 0, ['+', $lineno, ' ']; splice @f11, $i, 1, ['+', $lineno, $text]; last; } elsif ($sign eq '-') { splice @f11, $i, 1, ['-', $lineno, $text]; last; } } $i++ } } } my $cnt = 1; foreach (@f11) { print "$cnt: $_->[2]\n"; $cnt++; }
In reply to Highlighting source compare results by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |