On Dec 1, 2005, at 15:13, SG Edwards wrote:
Hi folks,
If I want to count the number of times that a match occurs in a
line is there a way of doing this, if there is I have not found it!
e.g.
$line="This is a sentence about matching sentences.\n";
$line=~/sentence/ig;
If you just want to count occurrences you can use a counter:
my $c = 0;
++$c while $line =~ /sentence/g;
There's a shorter idiom that takes advantage of the way list
assignments work in scalar context that involves something known as
the "goatse operator" (that's jargon it's not a real operator):
my $c =()= $line =~ /sentence/g;
but you have to choose which one is more easy to understand for you.
Is there a way to automatically capture each individual match (e.g.
using a special array character?)
I don't know whether this is what you ask for, but m//g in list
context returns the captures in one shot:
my @tags = $html =~ /<(\w+)/g;
That is documented in perlop.
-- fxn
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>