good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re^4: No tools? Use Perl?!by haukex (Archbishop) |
on Jul 29, 2016 at 07:14 UTC ( [id://1168798]=note: print w/replies, xml ) | Need Help?? |
Hi Boyd.Ako, I don't know the specifics as to how /g works. /g on an m// match is documented in perlop, in this case: "In scalar context, each execution of m//g finds the next match, returning true if it matches, and false if there is no further match." Since the regexp is being used in a while loop, without the /g modifier it would simply match the first occurrence every time and the loop would never end. With /g, the match advances through the string. Alternatively: "The /g modifier specifies global pattern matching--that is, matching as many times as possible within the string. ... In list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression." Without /g, it would only find the first match. When you add the /g and the capturing group to codiac's code it works:
Hope this helps,
In Section
Seekers of Perl Wisdom
|
|