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

Replies are listed 'Best First'.
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.