Re: Using an array to search a text file



On May 8, 3:52 pm, Uri Guttman <u...@xxxxxxxxxxxxxxx> wrote:
"PL" == Paul Lalli <mri...@xxxxxxxxx> writes:

PL> foreach my $prime_id ( @id_hits ) {
PL> while ( my $line = <$AFILE> ) {
PL> print $line if $line =~ /$prime_id/ ;
PL> }
PL> seek($AFILE, 0, 0); #rewind file when exhausted
PL> }

i don't know why no one has told you to invert the order of the loops
and scan the file once and the ids inside that. no seek is needed and it
will be much faster as the file is read in only one time.

while ( my $line = <$AFILE> ) {
foreach my $prime_id ( @id_hits ) {
print $line if $line =~ /$prime_id/ ;
}

}

Probably because that would rather drastically alter the order of the
output.... I'm making the assumption that the OP knows in what order
he wants the output. That's every line that contains the first
$prime_id, then every line that contains the second $prime_id, etc.
You've reversed that to the first line once for each $prime_id it
contains, then the second line once for each $prime_id it contains,
etc.

Paul Lalli

.