in reply to Difference between two arrays

My take on this would be to start at the end and work backwords.

#!/usr/bin/perl use warnings; use strict; my $before = [qw(1 2 3 4 5 6 7 8 9 10)]; my $after = [qw(2 3 4 5 1 6 7 8 9 10)]; my $len = @{$before}-1; for (reverse @{$before}){ print $before->[$len], "\t"; print $after->[$len], "\n"; last if $before->[$len] != $after->[$len]; $len--; } #$len++; print "offset: $len\n"; print "element: $after->[$len]\n";
output:
---------- Capture Output ---------- > "c:\perl\bin\perl.exe" _new.pl 10 10 9 9 8 8 7 7 6 6 5 1 offset: 4 element: 1 > Terminated with exit code 0.

Replies are listed 'Best First'.
Re^2: Difference between two arrays
by rafl (Friar) on Mar 22, 2006 at 12:12 UTC

    This works if the element is moved to an offset that is larget than its initial position. But what if it's being moved towards the beginning of the array?

    Cheers, Flo