in reply to comparing arrays
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/49831a95770a2ee5dicusses 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.
Chris#!/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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: comparing arrays
by nedals (Deacon) on Dec 17, 2004 at 06:55 UTC |