Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Help to improve script that searches a file for key words in a second file.

by space_monk (Chaplain)
on Mar 29, 2013 at 17:13 UTC ( #1026174=note: print w/ replies, xml ) Need Help??


in reply to Help to improve script that searches a file for key words in a second file.

Alternative method - Read the file in as one slurp, and count the number of occurrences of each term.

Something like this:

# read terms file open (SRC, $searchTermsFile) || die "can't open search terms file"; @terms = <SRC>; chomp (@terms); # read annotations file... open(my $f, '<', $file) or die "OPENING $file: $!\n"; my $string = do { local($/); <$f> }; close($f); my %nTermHits; foreach my $term (@terms) { $nTermHits{$term} = () = $string =~ /\b$term\b/ig; }
I don't have PERL on this machine so any minor syntax issues are your problem :o)
A Monk aims to give answers to those who have none, and to learn from those who know more.


Comment on Re: Help to improve script that searches a file for key words in a second file.
Download Code
Re^2: Help to improve script that searches a file for key words in a second file.
by mastarr (Novice) on Apr 01, 2013 at 19:29 UTC

    Thanks for the new method! I tried out your suggestion and it worked no problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026174]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-09-18 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (101 votes), past polls