in reply to searching 2 arrays
That "last" made you miss entries. To make you realize your mistake, here is the code you can compare with: (But this is not the solution to go for speed. Say the two arrays are of size n and m. In the worst case, this solution costs n*m, whereas use a hash (for DB) will only cost the size of the input data)
use strict; use warnings 'all'; my @DB = qw(AB1/1 AB1/5 AB2/5); my @Input = qw(AB1/1 AB1/2 AB1/5 AB1/6 AB1/9 AB2/2 AB2/5 AB2/6 AB2/9 A +B2/13); for my $InputData (@Input) { my $found; for my $DBData (@DB) { if ($DBData eq $InputData) { $found = 1; last; } } print "$InputData : " . ($found?" ":" Not") . "Found\n"; }
Which prints:
AB1/1 : Found AB1/2 : NotFound AB1/5 : Found AB1/6 : NotFound AB1/9 : NotFound AB2/2 : NotFound AB2/5 : Found AB2/6 : NotFound AB2/9 : NotFound AB2/13 : NotFound
Update: removed the extra brace liverpole found. Copy and Paste... Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching 2 arrays
by liverpole (Monsignor) on Oct 19, 2005 at 12:47 UTC |