in reply to REGEX:getting the count of occerence
foreach my $a(@arr) { $a =~ s/^\s*//; #trim leading spaces $a =~ s/\s*$//; #trim trailing spaces my $count = grep {$_ =~ /($a)/g} @text; print "\'$a\' found $count times\n"; }
The scalar form of grep returns the count of "matches" - not just a "true" value.
I think that is the main point that you missed.
If @arr contains multiple words, then I'm not know what you mean by a "match". I just trimmed the leading and trailing spaces from $a. If you have things in @arr like "Bob and Mary", and you want that to match across lines in @text, that is different too. But this will find " Bob and Mary " in an single line.
Your requirement is not well specified. Better would be to show some input and desired output.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: REGEX:getting the count of occerence
by ansh batra (Friar) on Jan 02, 2012 at 08:41 UTC |