On 03/14/2017 01:51 AM, Elizabeth Mattijsen wrote:
On 14 Mar 2017, at 02:04, ToddAndMargo <toddandma...@zoho.com> wrote:
On 03/13/2017 02:21 PM, Elizabeth Mattijsen wrote:
On 13 Mar 2017, at 22:17, ToddAndMargo <toddandma...@zoho.com> wrote:
I adore this feature of loops:

perl6 -e 'my @x=qw[a b z y];
 for @x -> $a, $b { say "<$a> <$b>" };'

<a> <b>
<z> <y>


because I can preassign a names to "$_".

Question:  in a pattern match such as:

perl6 -e 'my $x="ab12cd";
 $x ~~ m/(ab)(12)(cd)/;
 say "$x\n\$0=<$0>\t\$1=<$1>\t\$2=<$2>\n";'

ab12cd
$0=<ab>   $1=<12>   $2=<cd>

Is there a similar way to preassign names to
"$0", "$1", "$2" ?

$/[$n]
The answer is "no" to predefined variables, but as
Liz points out, there is a bit of a work around:

Note: the name of the variable is $<a> not $a

perl6 -e 'my $x="ab12cd"; $x ~~ m/$<a>=(ab) $<b>=(12) $<c>=(cd)/;
  say "$x\n\$a=<$<a>>\t\$b=<$<b>>\t\$c=<$<c>>\n";'

ab12cd
$a=<ab>   $b=<12>   $c=<cd>

I might be more readable to stick with $0, etc, as
you have to assign the <> variables to predefined
variables anyway

You can also slice $/:

    $ 6 'my $x=“ab12cd”; say "$<a b c>” if $x ~~ m/$<a>=(ab) $<b>=(12) 
$<c>=(cd)/'
    ab 12 cd

Please note that I put the slice between double quotes, otherwise you will see 
the gist of Match objects:

    $ 6 'my $x=“ab12cd”; say $<a b c> if $x ~~ m/$<a>=(ab) $<b>=(12) $<c>=(cd)/'
    (「ab」 「12」 「cd」)


HTH


Liz


Slowly but surely.  Every little bit helps.  Thank you!

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to