in reply to Re^2: diff of two strings
in thread diff of two strings
Sure. It is still not very polished but the algorithm should be quite optimal..Note that the brackets here are insertions and deletions, not new or common words! If you want this, I would mark these words in a postprocessing step. However, I think this information is not as important as insertions/deletions. They tell you exactly how to transform one string into the other. So I would use different colors for unique or common words.
my @output; printDiff($#s1, $#s2); print join "\n", @output; sub printDiff { my ($i, $j) = @_; if ($i > 0 && $j > 0 and $s1[$i] eq $s2[$j]) { printDiff($i-1, $j-1); $output[0] .= " " . $s1[$i]; $output[1] .= " " . $s1[$i]; } else { if ($j > 0 && ($i == 0 || $M[$i][$j-1] >= $M[$i-1][$j] +)) { printDiff($i, $j-1); $output[1] .= " <" . $s2[$j] . ">"; } elsif ($i > 0 && ($j == 0 || $M[$i][$j-1] < $M[$i-1][$ +j])) { printDiff($i-1, $j); $output[0] .= " [" . $s1[$i] . "]"; } } }
Perlmonks is the best [perl] community Perlmonks is <one> <of> the best community <of> <perl> <users>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: diff of two strings
by flaviusm (Acolyte) on Jan 09, 2008 at 05:17 UTC | |
by lima1 (Curate) on Jan 09, 2008 at 08:40 UTC |
In Section
Seekers of Perl Wisdom