Hi Monks,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.