Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: how to count matches

by ferreira (Chaplain)
on May 04, 2007 at 14:47 UTC ( #613583=note: print w/replies, xml ) Need Help??


in reply to how to count matches

When you did

my $d = ($n =~ /A/g)
the global match (triggered by /g) was done at scalar context. In this case it finds a single match or fails. So $d will return 1 or 0.

To do what you want, you need to set a list context and then count the matches.

my $n = "ABCDABDIDAOFOOFAA" ; my @matches = ($n =~ /A/g); my $d = @matches; print "matches $d";
will output "matches 5".

Replies are listed 'Best First'.
Re^2: how to count matches
by Anonymous Monk on Oct 24, 2015 at 10:42 UTC
    doesn't work :(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://613583]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-02-08 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (44 votes). Check out past polls.

    Notices?