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


In reply to Re^4: Difference between two arrays by johngg
in thread Difference between two arrays by rafl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.