Thanks much for the response. Though this isn't particularly about anything mentioned in your book, of course, I was starting with one of your examples then mutating it...
Moritz Lenz wrote: > When the match is in a different variable, you need to access the > capture group as $match[0] instead of its alias $/[0]. I had the feeling you might be saying something like this, that there was a way to access the 0th capture group like so: if $input ~~ / <$pattern> / { say $pattern[0]; # rx{ (\d+) \s+ (\w+) } say $input[0]; # There are 9 million bicycles in beijing. # Aside: funny there's no error: they're strings, not arrays } I would guess you meant something like this, though: if $input ~~ / <pattern=$pattern> / { say $<pattern>[0]; # 「9」 say $<pattern>[1]; # 「million」 } Which certainly works. On 3/11/21, Moritz Lenz <moritz.l...@gmail.com> wrote: > Hi there, > > On 11.03.21 17:43, William Michels wrote: >> Hi Moritz your book is mentioned below. Care to chime in? Reply to >> perl6-users <perl6-us...@perl.org> . >> >> Thx, Bill. >> W. Michels, Ph.D. >> >> ---------- Forwarded message --------- >> From: Joseph Brenner <doom...@gmail.com> >> Date: Thu, Mar 11, 2021 at 12:28 AM >> Subject: Working with a regex using positional captures stored in a >> variable >> To: perl6-users <perl6-us...@perl.org> >> >> >> Does this behavior make sense to anyone? When you've got a regex >> with captures in it, the captures don't work if the regex is >> stashed in a variable and then interpolated into a regex. >> >> Do capture groups need to be defined at the top level where the >> regex is used? >> >> { # From a code example in the "Parsing" book by Moritz Lenz, p. 48, >> section 5.2 >> my $input = 'There are 9 million bicycles in beijing.'; >> if $input ~~ / (\d+) \s+ (\w+) / { >> say $0.^name; # Match >> say $0; # 「9」 >> say $1.^name; # Match >> say $1; # 「million」 >> say $/; >> # 「9 million」 >> # 0 => 「9」 >> # 1 => 「million」 >> } >> } >> >> say '---'; >> >> { # Moving the pattern to var which we interpolate into match >> my $input = 'There are 9 million bicycles in beijing.'; >> my $pattern = rx{ (\d+) \s+ (\w+) }; >> if $input ~~ / <$pattern> / { >> say $0.^name; # Nil >> say $0; # Nil >> say $1.^name; # Nil >> say $1; # Nil >> say $/; # 「9 million」 >> } >> } >> >> In the second case, the match clearly works, but it behaves as >> though the capture groups aren't there. > > $0 is an alias for $/[0]. > > When the match is in a different variable, you need to access the > capture group as $match[0] instead of its alias $/[0]. > > Regards, > Moritz > > -- > Moritz Lenz > https://perlgeek.de/ -- https://raku.org/ >