I got the same set of pairs left in the arrays as TedPride, but I found a problem with his code in that he is splicing from the front of the array and that makes the indexes further along wrong, I believe. The reason it worked OK with this data set is because the only duplicate pair to be spliced from the arrays is the last element. I got warnings when I moved the duplicate from the last pos. in the arrays to the next to the last. But for some reason, the data came out OK (but with the warnings)! I eliminated the warnings with

for (reverse 0..$#array1)

change to the for loop.

Ned's works but I don't know why indexing a higher number

$i++
after a splice doesn't cause problems as the array keeps getting resized with splice. Hope someone might know. A link from c.l.p.m.,
http://groups-beta.google.com/group/comp.lang.perl.misc/msg/49831a95770a2ee5
dicusses this and there were a few links in my Perl Monks search, which seemed to indicate counting backwards in the for loop.

My solution walked from the end of the array to the front, splicing when duplicate pairs were found.

#!/usr/bin/perl use strict; use warnings; my @a1 = qw(13470660 13471850 14028274 14028286); my @a2 = qw(14028145 14028286 13476691 13471850); my %hash; for (reverse 0..$#a1) { # Does 2 checks. To see if a number from the second array was # already seen in the first array. Also, checks to see if its # a 'reversal' or flip flop and thus a duplicate. if (exists $hash{$a2[$_]} && $hash{$a2[$_]} == $a1[$_]) { splice @a1, $_, 1; splice @a2, $_, 1; } else { $hash{$a1[$_]} = $a2[$_]; } } print "@a1\n@a2\n";
Chris

In reply to Re: comparing arrays by Anonymous Monk
in thread comparing arrays by Anonymous Monk

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.