I have been struggling with a section of my code for a while now and can\'t figure it out. Seems to have something to do with how $1 is handled but I cannot find anything re
It's because you used if (//g)
instead of if (//)
.
//g
in scalar context sets pos($_)
[1] to where the match left off, or unsets pos($_)
[1] if the match was unsuccessful[2].//g
in scalar context starts matching at position pos($_)
[1].For example,
$_ = "ab";
say /(.)/g ? $1 : "no match"; # a
say /(.)/g ? $1 : "no match"; # b
say /(.)/g ? $1 : "no match"; # no match
say /(.)/g ? $1 : "no match"; # a
This allows the following to iterate through the matches:
while (/(.)/g) {
say $1;
}
Don't use if (//g)
[3]!
$_
is being used to represent the variable being matched against./c
is also used.while (//g)
, or unless you're using if (//gc)
to tokenize.