in reply to Difference between two arrays

If the arrays are not too big, I'd use a hash based solution:
my $i = $#{$before}; my %before = map { $before->[$_] => $_ } (0..$i); my %after = map { $after->[$_] => $_ } (0..$i); for (0..$i) { print "$before->[$_] has moved from $before{$before->[$_]} to $aft +er{$before->[$_]}\n" if $before{$before->[$_]} != $after{$before->[$_]}; } #output: #1 has moved from 0 to 4 #2 has moved from 1 to 0 #3 has moved from 2 to 1 #4 has moved from 3 to 2 #5 has moved from 4 to 3


holli, /regexed monk/

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

    That's actually a solution that could work, but I think it's rather ugly. Not ugly, as in "the code is bad", but it doesn't calculate the best solution which would only be one movement that needs to be done to go from $before to $after. So instead of one operation I'd need to do $#{$before} operations which obviously sucks as soon as the list gets bigger.

    Cheers, Flo