> 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
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;
So I will have matched "sentence" twice and I want to record this.
I can do it like this:
$line=~/sentence/ig;
# count the number of instances of this in the sentence
@split=split(/sentence/,$line);
$length=@split;
$length--;
This code works fine, but if I need to keep each matched term (as I
have wild-cards to capture spelling variants).
i.e. output would be:
sentence
sentece
Is there a way to automatically capture each individual match (e.g.
using a special array character?)
Sorry for the long-winded question, any help appreciated!
Is this close to what you want?
#!/usr/bin/perl
use strict;
use warnings;
my $line = "This is a sentence about matching sentences.\n";
my $count = 0;
while( $line =~ /(sentence\w*)/ig ){
my $lexical = $1;
$count ++;
print "$count. $lexical\n";
}
print "\ttotal: $count\n";
__END__
--
Just my 0.00000002 million dollars worth,
--- Shawn
"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
> 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
> I want to count the number of times that a match occurs
perldoc -q count
--
Affijn, Ruud
"Gewoon is een tijger."