Re: interpolating the return from embedded code in a regexp
> Anyway, I had quite forgotten that you had updated to 2020.05.1 (I thought > you were still on 2019.03.1). I still use the 2019.03 release pretty frequently, but I've got a newer release around I can check with. > FYI, I had been trying to write a line of code that calls the ".words" method on both the input lines Hm. Something like this? my $text = q:to/END/; P123 Green Labels That Say Magenta P666 Yoda puppets P912 Corn dogs END my @lines = $text.lines; my @words = $text.words; say @lines.elems; # 3 say @words.elems; # 12 say @words[5]; # Magenta On 6/18/20, William Michels wrote: > Hi Joe, > > Yup, your test file works perfectly: > > Last login: Thu Jun 18 19:38:12 on ttys000 > user@mbook:~$ perl6 regexp_code_interpolation_with_capture_var.t > ok 1 - case: {} $(... $0 ...) > ok 2 - case: <{... $0 ... .subst(/\s/, '\s', :g)}> > ok 3 - case: {} "... $0 ..." (brad gilbert rec) > ok 4 - case: {} $( ... ) with named capture > ok 5 - case: {} $( subcall( $0 ) ) > ok 6 - case: {} "&subcall( $0 )" > user@mbook:~$ perl6 --version > This is Rakudo version 2020.02.1..1 built on MoarVM version 2020.02.1 > implementing Raku 6.d. > user@mbook:~$ > > I'm sure I checked the previous code numerous times, so I'm wondering > if it's an issue specific to MoarVM version 2020.02.1 ? Anyway, I had > quite forgotten that you had updated to 2020.05.1 (I thought you were > still on 2019.03.1). > > FYI, I had been trying to write a line of code that calls the ".words" > method on both the input lines and your product list, but for some > reason I haven't been able to get to work. Maybe it's time for me to > update, as well. > > Best, Bill. > > > > On Thu, Jun 18, 2020 at 2:25 PM Joseph Brenner wrote: >> >> I don't have a 2020.02.1 around, but I see all of this working >> with both more recent and earlier versions: 2020.05.1 and 2019.03.1. >> >> > 1. I got your first "if" line (below) from June 14th to work, the one >> > you said, "it's not a complete solution": >> > 1> if $line ~~ / (^P\d+) \s+ <{ %products{$0}.subst(/\s+/, '\s', :g) }> >> > / { >> >> The reason it's "not complete", by the way is that it only covers the >> case of spaces when in principle the product descriptions might >> include any regexp meta-character, e.g. "*", "+", "(", ")", etc. Even >> a hyphen, "-" can mess things up. >> >> It's also pretty ugly, and arguably not complete in other ways, it >> would probably make more sense to drop the "+" in the subst pattern. >> And really every one of my examples should've pinned the whole >> expression with a trailing $ to make it an exact match on the >> description field: >> >> if $line ~~ / (^P\d+) \s+ <{ %products{$0}.subst(/\s/, '\s', :g) >> }> \s* $ / { >> >> I'm attaching a test file that exercises this stuff... if you get a >> chance, could you try to run it? It's supposed to say "ok" six times. >> >> >> On 6/17/20, William Michels wrote: >> > Hi Joe, >> > >> > 1. I got your first "if" line (below) from June 14th to work, the one >> > you said, "it's not a complete solution": >> > 1> if $line ~~ / (^P\d+) \s+ <{ %products{$0}.subst(/\s+/, '\s', :g) }> >> > / { >> > >> > 2. I got Brad's "if" line (below) from June 15th to work: >> > 2> if $line ~~ / (^P\d+) \s+ {} "%products{$0}" / { >> > >> > 3. However, I couldn't get the {}-updated "if" line (below) you posted >> > from June 17th to work: >> > 3> if / (^P\d+) \s+ {} $( %products{$0} ) / { >> > >> > 4. Nor could I get your named-capture "if" line (below) from June 17th >> > to >> > work: >> > 4> if / $=(^P\d+) \s+ >> > {} >> > $( %products{$.Str} ) / { >> > >> > By "works", I mean that the third "Corn dogs" example matches, while >> > the first two fail: >> > >> > checking line: P123 Viridian Green Label Saying Magenta >> > NO: bad line. >> > checking line: P666 Yoda puppets >> > NO: bad line. >> > checking line: P912 Corn dogs >> > Matched, line looks good >> > >> > "This is Rakudo version 2020.02.1..1 built on MoarVM version >> > 2020.02.1 implementing Raku 6.d." >> > >> > HTH, Bill. >> > >> > >> > >> > On Wed, Jun 17, 2020 at 1:13 PM Joseph Brenner >> > wrote: >> >> >> >> Brad Gilbert wrote: >> >> > You don't want to use <{…}>, you want to use "" >> >> >> >> > if $line ~~ / (^P\d+) \s+ {} "%products{$0}" / { >> >> >> >> Well, as contrived examples go this one could be >> >> improved. Instead of directly dereferencing a >> >> hash, maybe I should've used a sub call. >> >> >> >> > Note that {} is there to update $/ so that $0 works the way you >> >> > would >> >> > expect >> >> >> >> And notably you really need to know that trick to >> >> get that to work, but the direction I was >> >> going using <{ ... }> just works without it. >> >> >> >> I can confirm that the gratuitous code block trick fixes >> >> the approach I really thought should work: >> >> >> >>/ (^P\d+) \s+ {} $( %products{$0} ) / >> >> >> >> This had me wondering if named captures might work >> >> differently, but they
Re: interpolating the return from embedded code in a regexp
On Fri, Jun 19, 2020 at 12:12 AM Joseph Brenner wrote: > > > Anyway, I had quite forgotten that you had updated to 2020.05.1 (I thought > > you were > still on 2019.03.1). > > I still use the 2019.03 release pretty frequently, but I've got a > newer release around I can check with. > > > FYI, I had been trying to write a line of code that calls the ".words" > method on both the input lines > > Hm. Something like this? > > my $text = q:to/END/; > P123 Green Labels That Say Magenta > P666 Yoda puppets > P912 Corn dogs > END > > my @lines = $text.lines; > my @words = $text.words; > > say @lines.elems; # 3 > say @words.elems; # 12 > > say @words[5]; # Magenta Yes, I tried this line but it failed to match the third product ("Corn dogs"): #non-working code: if ($line.words.gist) ~~ m:g/ (^^P\d+) {} <{%products{$0}.words.gist}> / { (I see some blog posts showing the use of 'any' and 'all' junctions with smartmatch operators, so maybe I should have written an 'all' junction above). Best, Bill. > > > On 6/18/20, William Michels wrote: > > Hi Joe, > > > > Yup, your test file works perfectly: > > > > Last login: Thu Jun 18 19:38:12 on ttys000 > > user@mbook:~$ perl6 regexp_code_interpolation_with_capture_var.t > > ok 1 - case: {} $(... $0 ...) > > ok 2 - case: <{... $0 ... .subst(/\s/, '\s', :g)}> > > ok 3 - case: {} "... $0 ..." (brad gilbert rec) > > ok 4 - case: {} $( ... ) with named capture > > ok 5 - case: {} $( subcall( $0 ) ) > > ok 6 - case: {} "&subcall( $0 )" > > user@mbook:~$ perl6 --version > > This is Rakudo version 2020.02.1..1 built on MoarVM version 2020.02.1 > > implementing Raku 6.d. > > user@mbook:~$ > > > > I'm sure I checked the previous code numerous times, so I'm wondering > > if it's an issue specific to MoarVM version 2020.02.1 ? Anyway, I had > > quite forgotten that you had updated to 2020.05.1 (I thought you were > > still on 2019.03.1). > > > > FYI, I had been trying to write a line of code that calls the ".words" > > method on both the input lines and your product list, but for some > > reason I haven't been able to get to work. Maybe it's time for me to > > update, as well. > > > > Best, Bill. > > > > > > > > On Thu, Jun 18, 2020 at 2:25 PM Joseph Brenner wrote: > >> > >> I don't have a 2020.02.1 around, but I see all of this working > >> with both more recent and earlier versions: 2020.05.1 and 2019.03.1. > >> > >> > 1. I got your first "if" line (below) from June 14th to work, the one > >> > you said, "it's not a complete solution": > >> > 1> if $line ~~ / (^P\d+) \s+ <{ %products{$0}.subst(/\s+/, '\s', :g) }> > >> > / { > >> > >> The reason it's "not complete", by the way is that it only covers the > >> case of spaces when in principle the product descriptions might > >> include any regexp meta-character, e.g. "*", "+", "(", ")", etc. Even > >> a hyphen, "-" can mess things up. > >> > >> It's also pretty ugly, and arguably not complete in other ways, it > >> would probably make more sense to drop the "+" in the subst pattern. > >> And really every one of my examples should've pinned the whole > >> expression with a trailing $ to make it an exact match on the > >> description field: > >> > >> if $line ~~ / (^P\d+) \s+ <{ %products{$0}.subst(/\s/, '\s', :g) > >> }> \s* $ / { > >> > >> I'm attaching a test file that exercises this stuff... if you get a > >> chance, could you try to run it? It's supposed to say "ok" six times. > >> > >> > >> On 6/17/20, William Michels wrote: > >> > Hi Joe, > >> > > >> > 1. I got your first "if" line (below) from June 14th to work, the one > >> > you said, "it's not a complete solution": > >> > 1> if $line ~~ / (^P\d+) \s+ <{ %products{$0}.subst(/\s+/, '\s', :g) }> > >> > / { > >> > > >> > 2. I got Brad's "if" line (below) from June 15th to work: > >> > 2> if $line ~~ / (^P\d+) \s+ {} "%products{$0}" / { > >> > > >> > 3. However, I couldn't get the {}-updated "if" line (below) you posted > >> > from June 17th to work: > >> > 3> if / (^P\d+) \s+ {} $( %products{$0} ) / { > >> > > >> > 4. Nor could I get your named-capture "if" line (below) from June 17th > >> > to > >> > work: > >> > 4> if / $=(^P\d+) \s+ > >> > {} > >> > $( %products{$.Str} ) / { > >> > > >> > By "works", I mean that the third "Corn dogs" example matches, while > >> > the first two fail: > >> > > >> > checking line: P123 Viridian Green Label Saying Magenta > >> > NO: bad line. > >> > checking line: P666 Yoda puppets > >> > NO: bad line. > >> > checking line: P912 Corn dogs > >> > Matched, line looks good > >> > > >> > "This is Rakudo version 2020.02.1..1 built on MoarVM version > >> > 2020.02.1 implementing Raku 6.d." > >> > > >> > HTH, Bill. > >> > > >> > > >> > > >> > On Wed, Jun 17, 2020 at 1:13 PM Joseph Brenner > >> > wrote: > >> >> > >> >> Brad Gilbert wrote: > >> >> > You don't want to use <{…}>, you want to use "" > >> >> > >> >> > if $line ~~ / (^P\d+) \s+ {} "%products{$0}" / { > >> >> > >> >> Well, as contrive