Scott said:
I hope this isn't too dumb of a question, but what's the best way to
search for an array of items in the lines of a file? The following
seems inefficient, especially with a large number of search strings:
while($line=<FILE>){
foreach $s (@searchStrings){
push(@found,$line) if($line=~/$s/);
}
} ....
Scott
You might benefit from the use of the "study" function (perldoc -f
study) for your task. Check out the examples given there.
Also, if you are truly searching for strings rather than patterns,
consider using the "index" function instead of regexps. It should be
faster most of the time.
I assume from the way you coded it above that you know for certain your
strings do not contain any regexp metacharacters. If they do, you will
need to quote them using \Q and \E.
Don't build a new regexp every time through the file read loop -- that
requires each regexp to be compiled for each line. You could use the
alternation metacharacter | to build a single regexp that will match any
of your search strings, as was suggested by a previous poster. Or you
could build an array of regexps and later apply them one at a time.
Maybe like:
for(@searchStrings){push @regexps,qr/\Q$_\E/}
Then later:
while($line=~<FILE>){
study $line;
for(@regexps){push @found,$line if $line=~$_}
}
Finally, consider that a line may match more than one of your strings.
If that happens, your code will put the line in @found more than once.
Is that desired behavior? If not, you might want to terminate the
foreach loop when you have a successful match. That would also speed
things up, as additional matches would not be tested for once a match is
found.
HTH.