Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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 imbibing at the Monastery: (11)
As of 2015-07-03 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (51 votes), past polls