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