in reply to Counting words..
Wrt the wordcount, you say that you want to match a specific word, but all the solutions so far provided match strings. To illustrate the difference:
my $string = 'Cathy placated her cat, which was trying to catch a caterpillar.'; my $keyword = 'cat'; my $count = 0; ++$count while $string =~ /$keyword/gi; print "Occurrences of '$keyword': $count";
Output: Occurrences of 'cat': 5
If that's not what you want, wrap your keyword in word boundary metacharacters:
$string =~ /\b$keyword\b/gi;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Counting words..
by No-Lifer (Initiate) on Oct 24, 2005 at 16:50 UTC | |
by Roy Johnson (Monsignor) on Oct 24, 2005 at 17:11 UTC |