On 03/13/2017 04:12 PM, ToddAndMargo wrote:
On 03/13/2017 02:06 PM, ToddAndMargo wrote:
Hi All,

$ perl6 -e 'my $x="ab12cd"; $x ~~ m/ab(1q2)cd/; say "$x\n\$0=<$0>\n";'
Use of Nil in string context in block <unit> at -e line 1
ab12cd
$0=<>

With out the "q" in this, it works.  I deliberately put
the "q" to see what would happen when a patter was not
found.

Is there a way around the "use of nil" finger wag
if a patter is not found?

Or should I always test for its presence first if
there is a possibility the pattern might not exist?


Many thanks,
-T





Follow up.  My notes on matching:

Thank you all for the help!


Perl 6: Pattern Matching:

This Perl 5:
$ perl -e 'my $x="abc123def"; $x =~ m[(abc)(123)(def)]; print
"$x\n\$1=<$1>  \$2=<$2>  \$3=<$3>\n";'

abc123def
$1=<abc>  $2=<123>  $3=<def>


Translates in Perl 6 to:
Note: Perl 5 starts counting at 1 and Perl 6 starts counting at 0


$ perl6 -e 'my $x="abc123def"; $x ~~ m/(abc)(123)(def)/; say
"$x\n\$0=<$0>  \$1=<$1>  \$2=<$2>\n";'
abc123def
$0=<abc>  $1=<123>  $2=<def>


perl6 -e 'my $x="abc\(123\)def"; $x ~~ m/(abc)\((123)\)(def)/; say
"$x\n\$0=<$0>  \$1=<$1>  \$2=<$2>\n";'
abc(123)def
$0=<abc>  $1=<123>  $2=<def>



ooops!  No match!
$ perl6 -e 'my $x="ab12cd"; $x ~~ m/ab(1q2)cd/; say "$x\n\$0=<$0>\n";'
Use of Nil in string context
  in block <unit> at -e line 1
ab12cd
$0=<>

This is because you are trying to print out undefined variables.
To work around this:
   Reverse:
      perl6 -e 'my $x="ab12cd"; say "$x\n\$0=<$0>\n" if $x ~~
m/ab(1q2)cd/;'

   Forward:
      perl6 -e 'my $x="ab12cd"; if $x ~~ m/ab(1q2)cd/ { say
"$x\n\$0=<$0>\n"};'
Note: "ALL" matches have to be satisfied, or it will return "false"


pre-assigning variables, well sort of.  You can not assign an external
variable, like you can with a "for" loop.

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>




Add this to the list:

To grab something from the middle:

$ perl6 -e 'my $x="blah(good stuff 123)yuk";
     $x ~~ m |.*\((.*)\)|;
     say "$x\n\$0=<$0>";'

blah(good stuff 123)yuk
$0=<good stuff 123>




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

Reply via email to