in reply to Finding duplicate keys
Note that the code assumes that each "key" in the individual arrays are unique. Otherwise, it is possible that it'll skip over duplicated keys in cases such as:my @array1 = sort getArray1; my @array2 = sort getArray2; my ($i1,$i2) = (0,0); while (defined($array1[$i1]) && defined($array2[$i2])) { if ($array1[$i1] eq $array2[$i2]) { print "$array1[$i] is repeated at position array1[$i1] and array2[ +$i2]$/"; $i1++; $i2++; } if ($array1[$i1] > $array2[$i2]) { $i2++; } else { $i1++; } }
It will catch the first instance where the first a's appear. But, it will not find anything wrong with the a at $array1[1]. Of course, this case can easily be detected.@array1 = ( "a", "a", ... ); @array2 = ( "a", "b", ... ); Results: a is repeated at position array1[0] and array2[0]
antirice
The first rule of Perl club is - use Perl
The ith rule of Perl club is - follow rule i - 1 for i > 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Finding duplicate keys (cmp)
by Aristotle (Chancellor) on Apr 06, 2003 at 00:35 UTC | |
by antirice (Priest) on Apr 06, 2003 at 01:12 UTC |