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


in reply to Count multiple pattern matches

It's probably not the best way to do it but this is what sprang to mind when I saw this post. It's also very similar to what you already have.

use strict; my @keywords = qw/foo bar 12345 abcd/; my $string = "foobarfoo1234523423412345abcdefsadfabc"; my $re = join('|',@keywords); my %result; $result{$1}++ while ($string =~ /($re)/ig); foreach my $s (keys %result){ print "$s=>$result{$s}\n"; }