in reply to Re^3: Comparing $_ --- before/after
in thread Comparing $_ --- before/after

Yes. In your example, script should display
$difference = "oony owl";
as only starting "L" match both strings.

Replies are listed 'Best First'.
Re^5: Comparing $_ --- before/after
by zwon (Abbot) on Oct 10, 2009 at 11:06 UTC

    What about "o" in owl and dog? And what about backward difference? This is not exactly what String::Diff do. If you only want to get characters from second string which doesn't match to the characters in the first string it may be quite simple to implement, but I wouldn't call this a difference.

    Update: maybe you'll find the following (quick and dirty, sorry, I don't yet have my tea) code useful:

    use strict; use warnings; use 5.010; my $pair1 = ['abcdf', 'abcde']; my $pair2 = ['Lazy dog', 'Loony owl']; for my $ref ($pair1, $pair2) { say "Words: ", join ", ", @$ref; say "Diff: ", diff(@$ref); say "Diff (rev): ", diff(reverse @$ref); } sub diff { my ($str1, $str2) = @_; my @arr1 = split //, $str1; my @arr2 = split //, $str2; my @res = (); while ($_ = shift @arr2) { my $ch = shift @arr1 or last; push @res, $ch unless $ch eq $_; } push @res, @arr1; join "", @res; } __END__ Words: abcdf, abcde Diff: f Diff (rev): e Words: Lazy dog, Loony owl Diff: azy dg Diff (rev): oony wl