Re: What to do when a pattern match fails

2017-03-14 Thread Andy Bach
On Mon, Mar 13, 2017 at 8:40 PM, ToddAndMargo  wrote:

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

Just a further refinement - if you want only the stuff between inner parens
e.g.
my $x="blah(good stuff 123)yuk(more yuk)";

use the negated char class of the closing marker, in P5
 $x =~ m |.*\(([^)]*)\)|;
The initial ".*" isn't very helpful as:
 $x =~ m |\(([^)]*)\)|;

matches just as well.  I try to avoid untested matches, see the earlier
msg, but:
   if ( $x =~ m |.*\(([^)]*)\)|  ) {
# we got one
   }
   else {
 # no match
   }

Esp. (again P5) as, if the match fails, $0 et alia will continue to hold
what ever they had from before, e.g.
 my $y="blah(bad stuff abc)yuk(more yuk)";
 $y =~ m |.*\(([^)]*)\)|;  # $0 has "bad stuff abc
 my $x="blah[good stuff 123]yuk";   # wrong brackets
 $x =~ m |.*\(([^)]*)\)|;
# $0 still has "bad stuff abc"
 if ( defined $0 ) {
   # probably not what you wanted.


-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: What to do when a pattern match fails

2017-03-13 Thread ToddAndMargo

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  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=  $2=<123>  $3=


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=  $1=<123>  $2=


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=  $1=<123>  $2=



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  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 $ not $a

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





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=




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


Re: What to do when a pattern match fails

2017-03-13 Thread ToddAndMargo

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  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=  $2=<123>  $3=


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=  $1=<123>  $2=


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=  $1=<123>  $2=



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  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 $ not $a

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

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


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


Re: What to do when a pattern match fails

2017-03-13 Thread ToddAndMargo

On 03/13/2017 04:03 PM, yary wrote:


On Mon, Mar 13, 2017 at 6:16 PM, ToddAndMargo > wrote:

So if it only catches some of them, it will still return false?


There is no catching some of them- either the pattern matches and all
are caught, or the pattern fails and none are caught. If you can show us
an example of some matching, you'll probably find that some of the
matches are the empty string- go ahead an post if you have been seeing
that so we know what you're talking about exactly.

-y


I will post my note on this when I get one last piece for it.


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


Re: What to do when a pattern match fails

2017-03-13 Thread yary
On Mon, Mar 13, 2017 at 6:16 PM, ToddAndMargo  wrote:

> So if it only catches some of them, it will still return false?


There is no catching some of them- either the pattern matches and all are
caught, or the pattern fails and none are caught. If you can show us an
example of some matching, you'll probably find that some of the matches are
the empty string- go ahead an post if you have been seeing that so we know
what you're talking about exactly.

-y


Re: What to do when a pattern match fails

2017-03-13 Thread ToddAndMargo

On 03/13/2017 02:28 PM, Elizabeth Mattijsen wrote:



On 13 Mar 2017, at 22:20, ToddAndMargo  wrote:

On 03/13/2017 02:11 PM, Elizabeth Mattijsen wrote:



On 13 Mar 2017, at 22:06, 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  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?


The Nil is not caused by the smart match, but from your attempt to display the 
first positional capture (aka $0) of a match that failed.



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


You should test whether the smart match was successful.

   my $x="ab12cd"; say "$x\n\$0=<$0>\n” if $x ~~ m/ab(1q2)cd/;



Liz



Hi Liz,

Thank you!

How would you  do this if there were several matches in the line?

if $x ~~ m/(ab)(1q2)(cd)/;

Eventually break down an use .defined?


This is about the entire smart match, the result of the ~~ operator.  Either it 
returns true, and then everything is set in $/ (and it’s derived fields like 
$ if you’re using named captures).  Or it returns false, and then nothing 
is set, it’s all Nil.


HTH,


Liz



So if it only catches some of them, it will still return false?


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


Re: What to do when a pattern match fails

2017-03-13 Thread Elizabeth Mattijsen

> On 13 Mar 2017, at 22:20, ToddAndMargo  wrote:
> 
> On 03/13/2017 02:11 PM, Elizabeth Mattijsen wrote:
>> 
>>> On 13 Mar 2017, at 22:06, 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  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?
>> 
>> The Nil is not caused by the smart match, but from your attempt to display 
>> the first positional capture (aka $0) of a match that failed.
>> 
>> 
>>> Or should I always test for its presence first if
>>> there is a possibility the pattern might not exist?
>> 
>> You should test whether the smart match was successful.
>> 
>>my $x="ab12cd"; say "$x\n\$0=<$0>\n” if $x ~~ m/ab(1q2)cd/;
>> 
>> 
>> 
>> Liz
>> 
> 
> Hi Liz,
> 
> Thank you!
> 
> How would you  do this if there were several matches in the line?
> 
> if $x ~~ m/(ab)(1q2)(cd)/;
> 
> Eventually break down an use .defined?

This is about the entire smart match, the result of the ~~ operator.  Either it 
returns true, and then everything is set in $/ (and it’s derived fields like 
$ if you’re using named captures).  Or it returns false, and then nothing 
is set, it’s all Nil.


HTH,


Liz

Re: What to do when a pattern match fails

2017-03-13 Thread ToddAndMargo

On 03/13/2017 02:11 PM, Elizabeth Mattijsen wrote:



On 13 Mar 2017, at 22:06, 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  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?


The Nil is not caused by the smart match, but from your attempt to display the 
first positional capture (aka $0) of a match that failed.



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


You should test whether the smart match was successful.

my $x="ab12cd"; say "$x\n\$0=<$0>\n” if $x ~~ m/ab(1q2)cd/;



Liz



Hi Liz,

Thank you!

How would you  do this if there were several matches in the line?

if $x ~~ m/(ab)(1q2)(cd)/;

Eventually break down an use .defined?

Many thanks,
-T


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


Re: What to do when a pattern match fails

2017-03-13 Thread Elizabeth Mattijsen

> On 13 Mar 2017, at 22:06, 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  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?

The Nil is not caused by the smart match, but from your attempt to display the 
first positional capture (aka $0) of a match that failed.


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

You should test whether the smart match was successful.

my $x="ab12cd"; say "$x\n\$0=<$0>\n” if $x ~~ m/ab(1q2)cd/;



Liz