in reply to Count multiple pattern matches

removing previous keywords from a string should make your algoritm faster.
... foreach my $k (@keywords){ my $matches_count = ( ($string =~ s/$k//ig ) ); ...

Replies are listed 'Best First'.
Re^2: Count multiple pattern matches
by calin (Deacon) on Dec 08, 2004 at 15:22 UTC

    This won't work correctly if keywords overlap.

    For example, consider the string foops. This actually matches both foo and oops. But if you remove foo in the first iteration, the remaining string (ps) won't match the keyword oops, thus reducing the match count for oops from 1 (the correct result) to 0.