use 5.008; use strict; use warnings; # Data sample my @from = (1, 2, 4, 7); my @to = (1, 3, 4, 5, 7, 8); # Result my $count = 0; # number of changes my @added = (); # elements added in transition from @from to @to my @removed = (); # elements removed in transition from @from to @to # Algorithm my ($i, $j) = (0, 0); while ($i <= $#from or $j <= $#to) { if ($i > $#from or ($j <= $#to and $from[$i] > $to[$j])) { $count++; push @added, $to[$j++]; } elsif ($j > $#to or ($i <= $#from and $from[$i] < $to[$j])) { $count++; push @removed, $from[$i++]; } else { $i++; $j++; } } # Output { local $" = ', '; print "count: $count\n"; print "added: @added\n"; print "removed: @removed\n"; }