in reply to Re^2: Difference between two arrays
in thread Difference between two arrays

!= would be better

Noted and changed, thank you.

but the move is a valid one

Why do you think this is a valid move? The author's original post doesn't state anything like that, and the author's replies to other solutions suggests that this is not a valid move.

Update: I see the bug you were referring to and have updated my code accordingly. Thanks!

---
It's all fine and dandy until someone has to look at the code.

Replies are listed 'Best First'.
Re^4: Difference between two arrays
by johngg (Canon) on Mar 22, 2006 at 20:28 UTC
    Well, I thought it was a valid move because it is exactly what happens in the original post.

    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)];

    Element 0, the 1, has been spliced out then spliced back in as element 4. rafl also says in his first reply "the items can move forwards and backwards in the list." Also, nowhere in the posts is any constraint placed on where an element can be moved from or to.

    There is another wee bugette in that the sort to find $smallest should be a numerical one:-

    my $smallest = (sort {$a <=> $b} @array)[0];

    otherwise @array = (4 .. 13); would come up with 10 as it's "smallest" item because lexically 10 comes before 4.

    If we accept that we can move a number to the beginning of the list I still think there is a problem with your logic. If we start with

    my @array = (7,1,2,3,4,5,6,8,9);

    we get a $smallest of 1 and a $stored of 7 and $array[0] is also 1. Applying your logic, 7 != 1 && (7 + 1) != 1 is true and you exit with the error.

    Cheers

    JohnGG