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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.