Just off the top of my head (read: untested), I'd use a hash. If your IDs are truly that simple to parse, try this:
chomp ( my @id = <ID_LIST> ); my %ids; @id{@id} = (1) x @id; # dropped the chomp because you added \n back in while ( my $data = <DB> ) { my ($curr_id) = $data =~ /(user\d\d-\d\d\.dept\d\d)/; if ( $curr_id and exists $id{ $curr_id } ) { print OUT $data; } }
The problem you were having is that nested loops tend to not scale well. If your while loop iterates over 10 elements and you have 10 elements in @id, then you have a total of 100 (10 x 10) iterations. Bump that up to 100 items in the while loop and 100 elements in @id and you have 100 x 100 iterations: 10,000. Whenever you see something like that, the trick is to figure out how to remove one of the loops. Even if my single lookup on the hash key were horribly inefficient (for example, if my regex to extract the id had much backtracking), that would still probably scale better than a nested loop.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
In reply to Re: need more efficient way to write this query script
by Ovid
in thread need more efficient way to write this query script
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |