in reply to Iterating through Two Arrays. Is there a better use of memory?
If memory really is a problem (and I doubt it from the numbers you gave), you can could use
for (0..$#line2) { my $i = $line2[$_]; for (0..$#line1) { my $j = $line1[$_]; print $i if if $i eq $j; } }
If memory isn't a problem, you can use a much faster solution:
my %lookup; ++$lookup{$_} for @line2; for (@line1) { print if $lookup{$_}; }
Finally, you might want to get rid of duplicate messages.
my %lookup; $lookup{$_} = -1 for @line2; for (@line1) { print if !++$lookup{$_}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Iterating through Two Arrays. Is there a better use of memory?
by Jeri (Scribe) on Oct 13, 2011 at 17:47 UTC | |
by ikegami (Patriarch) on Oct 13, 2011 at 17:56 UTC |