Is there a VAR to count the number of occurences of matching

S

shrek11001

e.g.



$x =~ /A/g;



This var would provide the number of occurences of A in $x ...



Does it exist ? (according to perlvar, no, but ...)





- - -



I have that otherwise...



$x =~ s/[^A]//g;



$nocc = length($x);





but there shld be better...
 
G

Gunnar Hjalmarsson

shrek11001 said:
e.g.

$x =~ /A/g;

This var would provide the number of occurences of A in $x ...

Does it exist ? (according to perlvar, no, but ...)

You are asking a FAQ

perldoc -q count
 
E

Eric Schwartz

Gunnar Hjalmarsson said:
You are asking a FAQ

perldoc -q count

Not *quite*. His example is a FAQ, but the general problem of, "how
many times did this regex match?" is not one-- the FAQ only talks of
substrings.

my $regex = qr/(\.)\1/;
while($string =~ $regex)
$count++;
}
print "matched $count times\n";

is one approach.

-=Eric
 
M

Malcolm Dew-Jones

Eric Schwartz ([email protected]) wrote:
: > shrek11001 wrote:
: >> e.g.
: >> $x =~ /A/g;
: >> This var would provide the number of occurences of A in $x ...
: >> Does it exist ? (according to perlvar, no, but ...)
: >
: > You are asking a FAQ
: >
: > perldoc -q count

: Not *quite*. His example is a FAQ, but the general problem of, "how
: many times did this regex match?" is not one-- the FAQ only talks of
: substrings.

: my $regex = qr/(\.)\1/;
: while($string =~ $regex)
: $count++;
: }
: print "matched $count times\n";

: is one approach.

Or

$number_of_occurences_of_RE_in_x = () = $x=~m/$RE/g;
 
G

Gunnar Hjalmarsson

Eric said:
Not *quite*. His example is a FAQ, but the general problem of, "how
many times did this regex match?" is not one-- the FAQ only talks of
substrings.

my $regex = qr/(\.)\1/;
while($string =~ $regex)
$count++;
}
print "matched $count times\n";

is one approach.

If I haven't missed anything, that approach presupposes the use of the
/g modifier, so you cannot combine it with the qr// operator. This
works though:

while($string =~ /(\.)\1/g) {
$count++;
}
print "matched $count times\n";

Note that the FAQ answer to the question "How can I count the number
of occurrences of a substring within a string?" covers that approach
as well.
 
E

Eric Schwartz

Gunnar Hjalmarsson said:
Note that the FAQ answer to the question "How can I count the number
of occurrences of a substring within a string?" covers that approach
as well.

Of course it does. My excuse: I have a cold. It's either that, or
the ten million monkeys jumping up and down on my keyboard.

-=Eric
 
R

Roy Johnson

Or

$number_of_occurences_of_RE_in_x = () = $x=~m/$RE/g;

Or (oddly enough):
$number_of_occurences_of_RE_in_x = map(/$RE/g, $x);
which is somehow a little faster:

use strict;
use warnings;
use Benchmark;
my $str = 'aabaacbbacbabcbacbacbac';
my $re = qr/ac/;
my $num;

timethese(100_000, {
'mapmatch' => sub { $num = map(/$re/g, $str) },
'vialist' => sub { $num = () = $str =~ /$re/g }
});
$num = map(/$re/g, $str);
print "There are $num $re s in $str\n";
$num = () = $str =~ /$re/g;
print "There are $num $re s in $str\n";

--- output ---
Benchmark: timing 100000 iterations of mapmatch, vialist...
mapmatch: 1 wallclock secs ( 1.18 usr + 0.00 sys = 1.18 CPU) @
84745.76/s (n=100000)
vialist: 2 wallclock secs ( 1.80 usr + 0.00 sys = 1.80 CPU) @
55555.56/s (n=100000)
There are 5 (?-xism:ac) s in aabaacbbacbabcbacbacbac
There are 5 (?-xism:ac) s in aabaacbbacbabcbacbacbac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top