On Dec 1, SG Edwards said:

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!

$line="This is a sentence about matching sentences.\n";
$line=~/sentence/ig;

So I will have matched "sentence" twice and I want to record this.

No, you haven't. A pattern match in scalar context only matches ONCE. What you want to do is:

  my @matches = $line =~ /($pattern)/ig;

Assigning the return value of the pattern match to an array means that the pattern match is executed in LIST context (instead of scalar context), so it will match as many times as it can (due to the /g modifier).

--
Jeff "japhy" Pinyan        %  How can we ever be the sold short or
RPI Acacia Brother #734    %  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %    -- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to