halfcountplus has asked for the wisdom of the Perl Monks concerning the following question:

What's the easiest way to count the number of matches in one line? the context is something like this:
while (<DATA>) { if ($_ =~ /this/) { my $c=the number of times "this" occured print "$_\nthis matched: $c times" } }

Replies are listed 'Best First'.
Re: counting matches in a line
by FunkyMonk (Bishop) on Mar 16, 2008 at 17:06 UTC
Re: counting matches in a line
by pc88mxer (Vicar) on Mar 16, 2008 at 17:05 UTC
    Yet another question where there are many ways to do it. Here's just one:
    my $count; while (<DATA>) { s/(this)/$count++;$1/ge; # non-destructive version } print "number of this = $count\n";
Re: counting matches in a line
by halfcountplus (Hermit) on Mar 16, 2008 at 17:03 UTC
    never mind, i just noticed somebody asked this exact same question