http://www.perlmonks.org?node_id=1026172


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

if($annotation =~ /(\b($term)\b|$term-)\b|(-$term)\b/i) { $nTermHits {$term}++; $nTotalHits++; push(@annotationlist, "$annotation\n"); }

You are not using the contents of the capturing parentheses so you could simply write that as:

if($annotation =~ /(?:\b$term|$term-|-$term)\b/i) {


foreach $term (@terms) { if ($nTermHits {$term} > 0) { printf "%s\t%d\n", $term, $nTermHits {$term}; } }

That would be better as:

foreach $term (keys %nTermHits) { printf "%s\t%d\n", $term, $nTermHits {$term}; }

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 18:31 UTC

    Thanks. That was exactly what I was looking for.