Hello,

I searched for a solution to my problem on this site already, but for some reason all nodes I found talked about the synetric difference of two arrays, which is not what I want. Maybe the problem has another name?

However, here is the problem. There are two arrays given (array references in that case):

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

The first array always looks the same (ascending numbers from $x to $x + $y). The second array is based on the first array, but one element inside it was moved. In the code example I gave it was element 0 (value 1) that was moved to position 5. The rest of the elements in $after moved up and down automatically.

The operation that was executed on the array could be expressed that way in perl:

my $element = splice(@$after, 0, 1); #remove element 0; splice(@$after, 4, 0, $element); #insert element at offset 4

The question is how to find out which element was moved to which offset, given that you only know the array $before and $after.

TIA, Flo


In reply to 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.