Re: Fwd: Working with a regex using positional captures stored in a variableperl6-us...@perl.org

2021-03-11 Thread Moritz Lenz
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  .
> 
> Thx, Bill.
> W. Michels, Ph.D.
> 
> -- Forwarded message -
> From: Joseph Brenner 
> Date: Thu, Mar 11, 2021 at 12:28 AM
> Subject: Working with a regex using positional captures stored in a variable
> To: perl6-users 
> 
> 
> 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/


Re: A problem case for the site documentation search: the "^methods" method.

2021-03-11 Thread Richard Hainsworth

Jo,

For what its worth, the search page on http://raku.finanalyst.org now 
takes you to ^methods


Previously, a search on '^methods' found the reference, but hide the 
location, so the link could not be clicked. That is fixed now.


Regards,

Richard

On 10/03/2021 15:42, Joseph Brenner wrote:

JJ Merelo  wrote:

This is a bug in Documentable. Generated an issue there
https://github.com/Raku/Documentable/issues/149 Thanks for the report.

And thanks for opening the issue.


"rule" declarator: Different results for 'unadorned' match vs unnamed/named captures? (in re Grammars...).

2021-03-11 Thread William Michels via perl6-users
Hello,

I've been chatting with raiph on SO regarding Grammar "tokens" vs
"rules". The OP is here https://stackoverflow.com/q/62051742 and our
discussion is here https://stackoverflow.com/a/62053666 .

I think there's something going on with the examples below, as I'm
seeing different results when comparing a basic "rule" match vs either
unnamed or named "rule" captures. The focus of the tests below is to
look at changes with/without whitespace, and comparing "tokens" vs
"rules".

TL;DR version:

> say $/ if 'ab'  ~~ rule  {.? .?}
「a」
> say $/ if 'ab'  ~~ rule  {(.?) (.?)}
「b」
 0 => 「b」
 1 => 「」
> say $/ if 'ab'  ~~ rule  {$=.? $=.?}
「b」
 first => 「b」
 second => 「」

So for the first example (an unadorned "rule" match), Raku returns 「a」.
But for the second and third examples ("rule" captures), Raku returns 「b」.
(Also confirmed the three lines above with Raku one-liners).

Full list of examples:

Last login: Thu Mar 11 11:50:11 on ttys042
user@mbook:~$ raku
Welcome to 퐑퐚퐤퐮퐝퐨™ v2020.10.
Implementing the 퐑퐚퐤퐮™ programming language v6.d.
Built on MoarVM version 2020.10.

To exit type 'exit' or '^D'
> say $/ if 'a'  ~~ token  {.?.?}
「a」
> say $/ if 'ab'  ~~ token  {.?.?}
「ab」
> say $/ if 'ab'  ~~ token  {.? .?}
「ab」
> say $/ if 'a b'  ~~ token  {.? .?}
「a 」
> say $/ if 'a'  ~~ rule  {.?.?}
「a」
> say $/ if 'ab'  ~~ rule  {.?.?}
「ab」
> say $/ if 'ab'  ~~ rule  {.? .?}
「a」
> say $/ if 'a b'  ~~ rule  {.? .?}
「a b」
> 
Nil
> say $/ if 'a'  ~~ token  {(.?)(.?)}
「a」
 0 => 「a」
 1 => 「」
> say $/ if 'ab'  ~~ token  {(.?)(.?)}
「ab」
 0 => 「a」
 1 => 「b」
> say $/ if 'ab'  ~~ token  {(.?) (.?)}
「ab」
 0 => 「a」
 1 => 「b」
> say $/ if 'a b'  ~~ token  {(.?) (.?)}
「a 」
 0 => 「a」
 1 => 「 」
> say $/ if 'a'  ~~ rule  {(.?)(.?)}
「a」
 0 => 「a」
 1 => 「」
> say $/ if 'ab'  ~~ rule  {(.?)(.?)}
「ab」
 0 => 「a」
 1 => 「b」
> say $/ if 'ab'  ~~ rule  {(.?) (.?)}
「b」
 0 => 「b」
 1 => 「」
> say $/ if 'a b'  ~~ rule  {(.?) (.?)}
「a b」
 0 => 「a」
 1 => 「b」
> 
Nil
> say $/ if 'a'  ~~ token  {$=.?$=.?}
「a」
 first => 「a」
 second => 「」
> say $/ if 'ab'  ~~ token  {$=.?$=.?}
「ab」
 first => 「a」
 second => 「b」
> say $/ if 'ab'  ~~ token  {$=.? $=.?}
「ab」
 first => 「a」
 second => 「b」
> say $/ if 'a b'  ~~ token  {$=.? $=.?}
「a 」
 first => 「a」
 second => 「 」
> say $/ if 'a'  ~~ rule  {$=.?$=.?}
「a」
 first => 「a」
 second => 「」
> say $/ if 'ab'  ~~ rule  {$=.?$=.?}
「ab」
 first => 「a」
 second => 「b」
> say $/ if 'ab'  ~~ rule  {$=.? $=.?}
「b」
 first => 「b」
 second => 「」
> say $/ if 'a b'  ~~ rule  {$=.? $=.?}
「a b」
 first => 「a」
 second => 「b」
> 
Nil

Any advice appreciated, Thx, Bill.


The SF Perl Raku Study Group, 03/14 at 1pm PDT

2021-03-11 Thread Joseph Brenner
Donald Knuth, "Computer programming as an art", CACM, December 1974:

"In this connection it is most important for us all
to remember that there is no one 'best' style; everybody
has his own preferences, and it is a mistake to try to force
people into an unnatural mold.  ...  The important thing is
that you really *like* the style you are using; it should be
the best way you prefer to express yourself."

The Raku Study Group

March 14th, 2021  1pm in California, 9pm in the UK

Zoom meeting link:
  https://us02web.zoom.us/j/88493580999?pwd=Zmt3bUE0SFZLYU9MRnFDSXY5eDJLQT09

Passcode: 4RakuRoll

RSVPs are useful, though not needed:
  https://www.meetup.com/San-Francisco-Perl/events/276873465/


Re: Working with a regex using positional captures stored in a variable

2021-03-11 Thread Brad Gilbert
If you interpolate a regex, it is a sub regex.

If you have something like a sigil, then the match data structure gets
thrown away.

You can put it back in as a named

> $input ~~ / 
「9 million」
 pattern => 「9 million」
  0 => 「9」
  1 => 「million」

Or as a numbered:

> $input ~~ / $0 = <$pattern>
「9 million」
 0 => 「9 million」
  0 => 「9」
  1 => 「million」

Or put it in as a lexical regex

> my regex pattern { (\d+) \s+ (\w+) }
> $input ~~ /   /
「9 million」
 pattern => 「9 million」
  0 => 「9」
  1 => 「million」

Or just use it as the whole regex

> $input ~~ $pattern # variable
「9 million」
 0 => 「9」
 1 => 「million」

> $input ~~  # my regex pattern /…/
「9 million」
 0 => 「9」
 1 => 「million」

On Thu, Mar 11, 2021 at 2:29 AM Joseph Brenner  wrote:

> 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.
>
>
>raku --version
>
>Welcome to 퐑퐚퐤퐮퐝퐨™ v2020.10.
>Implementing the 퐑퐚퐤퐮™ programming language v6.d.
>


Working with a regex using positional captures stored in a variable

2021-03-11 Thread Joseph Brenner
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.


   raku --version

   Welcome to 퐑퐚퐤퐮퐝퐨™ v2020.10.
   Implementing the 퐑퐚퐤퐮™ programming language v6.d.