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

Re: Error in my Regular expression pattern

by Anonymous Monk
on Mar 18, 2012 at 07:13 UTC ( #960266=note: print w/ replies, xml ) Need Help??

Comment on Re: Error in my Regular expression pattern
Download Code
Re^2: Error in my Regular expression pattern
by Anonymous Monk on Mar 18, 2012 at 07:28 UTC
    Like perlop#m/PATTERN/msixpodualgc ( perlop#m// ), says,
    m// in scalar context returns true if it succeeds, false if it fails.

    Each time m// succeeds against a variable, like when  $n = m/twinkle/ig; executes, position is changed, so that  while(/twinkle/ig) { executes, only the second twinkle is counted.

    If you add this to your program before the while loop

    print "pos is ", pos, ", remainder '", substr($_, pos), "'\n"; pos($_) = undef;
    this resets pos associated with $_, and then your program will produce
    Number of occurences = 1 pos is 7, remainder ' twinkle little star' Found 2 times

    Like perlfaq4 says, if you want the number of occurances, use the flying lentil operator, use  my $count =()= m/twinkle/ig;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2015-07-04 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls