Re: Variable character class

2019-09-08 Thread Laurent Rosenfeld via perl6-users
Because sets are unordered collections of items. And the (&) operator
returns a set.



Le dim. 8 sept. 2019 à 00:18, Aureliano Guedes 
a écrit :

> Why does it dont return ordered for neither of those lists?
> I mean:
>
> my $u = "24680";
> my @s = $u.comb.unique;
> say @s ; # [2 4 6 8 0]
>
> sub matching_chars(Str $a, Str $b) {
>my @c = $a.comb.unique;
>my @d = $b.comb.unique;
>return ~[@c (&) @d];
> }
>
> say matching_chars("24680", "1234567890"); # says 2 0 8 4
>
> On Mon, Sep 2, 2019 at 1:20 AM William Michels via perl6-users <
> perl6-us...@perl.org> wrote:
>
>> Thanks Simon, good point. I ran into the same trouble as others trying
>> to get the answer via regex, and switched over to sets as an
>> alternative. I'll confess I completely missed that Yary's Perl5 code
>> returned the substring "8420" present in his test "19584203" string,
>> and that was the answer he was going for.
>>
>> Simon and Eirik, I still think there might be some value in calling
>> the .unique method on $chars.comb as in "$chars.comb.unique". But I
>> haven't had the opportunity to check code efficiency  +/-  the
>> ".unique" method call.
>>
>> For those interested in using Sets as a solution, you can stringifying
>> the 'return' line (below). Still working on using "join()" to
>> concatenate!
>>
>> sub matching_chars(Str $a, Str $b) {
>>my @c = $a.comb.unique;
>>my @d = $b.comb.unique;
>>#return (~[@c (&) @d]).^name;
>>return ~[@c (&) @d];
>> }
>>
>> say matching_chars("24680", "19584203"); # says 2 0 8 4
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says [ + ] /
>> say matching_chars("Lorem ipsum dolor sit amet, consectetuer
>> adipiscing elit.", "abcdef"); # says c a d e
>>
>>
>> HTH, Bill.
>>
>> PS. Simon, thanks for all the great Youtube videos you've done from
>> LondonPM!
>>
>> https://youtu.be/yt8SrZ_V_50
>> https://youtu.be/9M1xZQ0_Skw
>>
>>
>>
>>
>>
>>
>>
>>
>> On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor 
>> wrote:
>> >
>> > Using a set would be good but it doesn't give you the matching string
>> from the original (which is what I thought was required) otherwise Sets
>> would be my first thought.
>> >
>> > On Sun, 1 Sep 2019, 17:57 William Michels, 
>> wrote:
>> >>
>> >> Hi Yary and Paul and Simon,
>> >>
>> >> I ran into the same difficulties as Yary with repeated characters, so
>> >> I tried the .unique method. Then after a while, I realized that
>> >> problems like this might best be treated as "Set" problems in Perl6.
>> >> Note the Set Intersection operator "(&)" below:
>> >>
>> >> sub matching_chars(Str $a, Str $b) {
>> >>my @c = $a.comb.unique;
>> >>my @d = $b.comb.unique;
>> >>#say @c; say @d;
>> >>return @c (&) @d;
>> >> }
>> >>
>> >> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
>> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>> >>
>> >>
>> >> https://docs.perl6.org/routine/Set
>> >> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>> >>
>> >> HTH, Bill.
>> >>
>> >>
>> >> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci 
>> wrote:
>> >> >
>> >> > I'm not entirely sure if this is the correct answer, but if you
>> define your own custom character class
>> >> > with a 'regex' object, you can use that in the grouping.
>> >> >
>> >> > sub matching_chars(Str $chars_to_match, Str $_) {
>> >> > my regex x { $chars_to_match ** 1 };
>> >> > m/<[]>/;
>> >> > }
>> >> >
>> >> > The above worked for me in the very small testing I did.
>> >> >
>> >> > ~Paul
>> >> >
>> >> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>> >> >>
>> >> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
>> specifying a character class via a variable.
>> >> >>
>> >> >> Perl 5:
>> >> >> sub matching_chars {
>> >> >>   (my $chars_to_match, local $_) = @_;
>> >> >>   /([$chars_to_match]+)/
>> >> >> }
>> >> >>
>> >> >> say matching_chars('24680', '19584203'); # says 8420
>> >> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>> >> >>
>> >> >> Perl 6:
>> >> >> sub matching_chars(Str $chars_to_match, Str $_) {
>> >> >> # warnings, treats as string not variable
>> >> >> m/<[$chars_to_match]>/;
>> >> >> }
>> >> >>
>> >> >> How do I get Perl 6 to interpret a variable in the contents of a
>> character class?
>> >> >> From http://docs.perl6.org/language/regexes#Regex_interpolation
>> I'd think that  Rakudo would use the literal contents of $chars_to_match,
>> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
>> warning about repeated c, underscore, etc.
>> >> >>
>> >> >> -y
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > __
>> >> >
>> >> > :(){ :|:& };:
>>
>
>
> --
> Aureliano Guedes
> skype: aureliano.guedes
> contato:  (11) 94292-6110
> whatsapp +5511942926110
>


Re: Variable character class

2019-09-07 Thread Aureliano Guedes
Why does it dont return ordered for neither of those lists?
I mean:

my $u = "24680";
my @s = $u.comb.unique;
say @s ; # [2 4 6 8 0]

sub matching_chars(Str $a, Str $b) {
   my @c = $a.comb.unique;
   my @d = $b.comb.unique;
   return ~[@c (&) @d];
}

say matching_chars("24680", "1234567890"); # says 2 0 8 4

On Mon, Sep 2, 2019 at 1:20 AM William Michels via perl6-users <
perl6-us...@perl.org> wrote:

> Thanks Simon, good point. I ran into the same trouble as others trying
> to get the answer via regex, and switched over to sets as an
> alternative. I'll confess I completely missed that Yary's Perl5 code
> returned the substring "8420" present in his test "19584203" string,
> and that was the answer he was going for.
>
> Simon and Eirik, I still think there might be some value in calling
> the .unique method on $chars.comb as in "$chars.comb.unique". But I
> haven't had the opportunity to check code efficiency  +/-  the
> ".unique" method call.
>
> For those interested in using Sets as a solution, you can stringifying
> the 'return' line (below). Still working on using "join()" to
> concatenate!
>
> sub matching_chars(Str $a, Str $b) {
>my @c = $a.comb.unique;
>my @d = $b.comb.unique;
>#return (~[@c (&) @d]).^name;
>return ~[@c (&) @d];
> }
>
> say matching_chars("24680", "19584203"); # says 2 0 8 4
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says [ + ] /
> say matching_chars("Lorem ipsum dolor sit amet, consectetuer
> adipiscing elit.", "abcdef"); # says c a d e
>
>
> HTH, Bill.
>
> PS. Simon, thanks for all the great Youtube videos you've done from
> LondonPM!
>
> https://youtu.be/yt8SrZ_V_50
> https://youtu.be/9M1xZQ0_Skw
>
>
>
>
>
>
>
>
> On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor 
> wrote:
> >
> > Using a set would be good but it doesn't give you the matching string
> from the original (which is what I thought was required) otherwise Sets
> would be my first thought.
> >
> > On Sun, 1 Sep 2019, 17:57 William Michels, 
> wrote:
> >>
> >> Hi Yary and Paul and Simon,
> >>
> >> I ran into the same difficulties as Yary with repeated characters, so
> >> I tried the .unique method. Then after a while, I realized that
> >> problems like this might best be treated as "Set" problems in Perl6.
> >> Note the Set Intersection operator "(&)" below:
> >>
> >> sub matching_chars(Str $a, Str $b) {
> >>my @c = $a.comb.unique;
> >>my @d = $b.comb.unique;
> >>#say @c; say @d;
> >>return @c (&) @d;
> >> }
> >>
> >> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
> >>
> >>
> >> https://docs.perl6.org/routine/Set
> >> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
> >>
> >> HTH, Bill.
> >>
> >>
> >> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci 
> wrote:
> >> >
> >> > I'm not entirely sure if this is the correct answer, but if you
> define your own custom character class
> >> > with a 'regex' object, you can use that in the grouping.
> >> >
> >> > sub matching_chars(Str $chars_to_match, Str $_) {
> >> > my regex x { $chars_to_match ** 1 };
> >> > m/<[]>/;
> >> > }
> >> >
> >> > The above worked for me in the very small testing I did.
> >> >
> >> > ~Paul
> >> >
> >> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
> >> >>
> >> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
> specifying a character class via a variable.
> >> >>
> >> >> Perl 5:
> >> >> sub matching_chars {
> >> >>   (my $chars_to_match, local $_) = @_;
> >> >>   /([$chars_to_match]+)/
> >> >> }
> >> >>
> >> >> say matching_chars('24680', '19584203'); # says 8420
> >> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
> >> >>
> >> >> Perl 6:
> >> >> sub matching_chars(Str $chars_to_match, Str $_) {
> >> >> # warnings, treats as string not variable
> >> >> m/<[$chars_to_match]>/;
> >> >> }
> >> >>
> >> >> How do I get Perl 6 to interpret a variable in the contents of a
> character class?
> >> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
> think that  Rakudo would use the literal contents of $chars_to_match,
> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
> warning about repeated c, underscore, etc.
> >> >>
> >> >> -y
> >> >
> >> >
> >> >
> >> > --
> >> > __
> >> >
> >> > :(){ :|:& };:
>


-- 
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110


Re: Variable character class

2019-09-05 Thread William Michels via perl6-users
Hi Gianni,

I think we're looking at two different aspects of the same problem.
I'm just concerned with learning Perl6 regexes. Presumably many people
will continue to use Perl5 regexes for a long time to come (for
reasons of familiarity, and/or speed).

Regarding Perl6 regexes, your last two P6 examples are below, with
added variants. I don't see any difference between wrapping the string
that becomes $target in single-quotes or with Q{...}. See first four
examples below:

say matching_chars('+/][', 'Apple ][+//\\e'); #says (「][+//」)
say matching_chars('+/][', Q{Apple ][+//\\e}); #says (「][+//」)
say matching_chars('+\/][', 'Apple ][+//\\e'); #says (「][+//\\」)
say matching_chars('+\/][', Q{Apple ][+//\\e}); #says (「][+//\\」)

OTOH, I can eliminate backslashes from the smartmatch result by simply
double-quoting the input string that becomes $chars_to_match (third
and fourth examples below). At the same time, double-quoting the
string that becomes $target doesn't alter the 'goal' of eliminating
backslashes from the smartmatch result:

say matching_chars('+\/][', 'Apple ][+//\\e'); #says (「][+//\」)
say matching_chars('+\/][', "Apple ][+//\\e"); #says (「][+//\」)
say matching_chars("+\/][", 'Apple ][+//\\e'); #says (「][+//」)
say matching_chars("+\/][", "Apple ][+//\\e"); #says (「][+//」)

This P6 way seems pretty clever--and also easy to remember--to me.
Does it seem that way to you? Or do you still find P5 more suitable?

Best Regards, Bill.



On Thu, Sep 5, 2019 at 6:25 AM Gianni Ceccarelli  wrote:
>
> On Wed, 4 Sep 2019 21:44:29 -0700
> William Michels via perl6-users  wrote:
>
> > Hi Gianni, I'm not sure of the Perl5 case, but what you're saying is,
> > if your target string is backslashed, be sure to "quote-interpolate
> > it" in Perl6? (see below):
>
> Re-reading what I wrote, I realise it was really not clear. Let me try
> again.
>
> yary's original code was::
>
>   sub matching_chars {
> (my $chars_to_match, local $_) = @_;
> /([$chars_to_match]+)/
>   }
>   say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>
> If ``matching_chars`` took "a set of characters expressed as a string"
> as its first argument, you'd expect to be able to say::
>
>   say matching_chars('+/][', 'Apple ][+//e');
>
> but this doesn't work, because it tells Perl5 to compile this regex::
>
>   ([+/][])+
>
> which doesn't compile ("Unmatched [ in regex").
>
> Sidhekin's Perl6 version doesn't have this issue. On the other hand,
> this happens::
>
>   sub matching_chars(Str $chars_to_match, Str $target) {
> my @chars-to-match = $chars_to_match.comb;
> return $target ~~ m:g/@chars-to-match + /;
>   }
>
>   is matching_chars('+/][', Q{Apple ][+//\\e}),['][+//'];
>   is matching_chars('+\/][', Q{Apple ][+//\\e}),[Q{][+//\\}];
>
>
> The "fixed" Perl5 version would be::
>
>   /([\Q$chars_to_match\E]+)/
>
> or, equivalently::
>
>   $chars_to_match = quotemeta($chars_to_match);
>   /([$chars_to_match]+)/
>
> Does that help?
>
> --
> Dakkar - 
> GPG public key fingerprint = A071 E618 DD2C 5901 9574
>  6FE2 40EA 9883 7519 3F88
> key id = 0x75193F88
>
> You have literary talent that you should take pains to develop.


Re: Variable character class

2019-09-05 Thread Gianni Ceccarelli
On Wed, 4 Sep 2019 21:44:29 -0700
William Michels via perl6-users  wrote:

> Hi Gianni, I'm not sure of the Perl5 case, but what you're saying is,
> if your target string is backslashed, be sure to "quote-interpolate
> it" in Perl6? (see below):

Re-reading what I wrote, I realise it was really not clear. Let me try
again.

yary's original code was::

  sub matching_chars {
(my $chars_to_match, local $_) = @_;
/([$chars_to_match]+)/
  }
  say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//

If ``matching_chars`` took "a set of characters expressed as a string"
as its first argument, you'd expect to be able to say::

  say matching_chars('+/][', 'Apple ][+//e');

but this doesn't work, because it tells Perl5 to compile this regex::

  ([+/][])+

which doesn't compile ("Unmatched [ in regex").

Sidhekin's Perl6 version doesn't have this issue. On the other hand,
this happens::

  sub matching_chars(Str $chars_to_match, Str $target) {
my @chars-to-match = $chars_to_match.comb;
return $target ~~ m:g/@chars-to-match + /;
  }

  is matching_chars('+/][', Q{Apple ][+//\\e}),['][+//'];
  is matching_chars('+\/][', Q{Apple ][+//\\e}),[Q{][+//\\}];


The "fixed" Perl5 version would be::

  /([\Q$chars_to_match\E]+)/

or, equivalently::

  $chars_to_match = quotemeta($chars_to_match);
  /([$chars_to_match]+)/

Does that help?

-- 
Dakkar - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88

You have literary talent that you should take pains to develop.


Re: Variable character class

2019-09-04 Thread William Michels via perl6-users
Hi Gianni, I'm not sure of the Perl5 case, but what you're saying is,
if your target string is backslashed, be sure to "quote-interpolate
it" in Perl6? (see below):

say matching_chars( '+/][',  'Apple ][+//e' ); # says (「][+//」)
say matching_chars( '+\/\]\[', 'Apple ][+//e' ); # says (「][+//」)
say matching_chars( '+/][', 'Apple \]\[+\/\/e' ); # (「]」 「[+」 「/」 「/」)
say matching_chars( '+\/\]\[', 'Apple \]\[+\/\/e' ); # (「\]\[+\/\/」)

#say matching_chars( '+/][', q'Apple \]\[+\/\/e' ); # says error
say matching_chars( '+/][', q/  Apple \]\[+\/\/e  / ); # says (「]」 「[+//」)
#say matching_chars( '+/][', q:b'Apple \]\[+\/\/e' ); # says error
say matching_chars( '+/][', q:b/ Apple \]\[+\/\/e / ); # says (「][+//」)
say matching_chars( '+/][', "Apple \]\[+\/\/e" ); # says (「][+//」)

HTH, Bill.




On Tue, Sep 3, 2019 at 9:51 AM Gianni Ceccarelli  wrote:
>
> On Tue, 3 Sep 2019 09:15:54 -0700
> William Michels via perl6-users  wrote:
>
> > Just a short note that Eirik's array-based code seems to work fine,
> > with-or-without backslash-escaping the first input string (minimal
> > testing, below):
>
> Oh, sure. But when the target string contains backslashes, it will
> behave differently from the P5 version, in that the P5 version won't
> report the backslashes as matching (because of the way it builds its
> regex).
>
>
> --
> Dakkar - 
> GPG public key fingerprint = A071 E618 DD2C 5901 9574
>  6FE2 40EA 9883 7519 3F88
> key id = 0x75193F88


Re: Variable character class

2019-09-03 Thread William Michels via perl6-users
I'm wrong then. Nowhere on that reference page does the character
construction "<{...}>" (block wrapped in angle brackets) appear.

Per your reference, "pointy-blocks" seems to refer to an arrow in
conjunction with a block, as mentioned three times on the
'Python-to-Perl6' page:

https://docs.perl6.org/language/py-nutshell

Nota bene: the opposite character construction (angle brackets wrapped
in a block) DOES appear on your reference page--it's used to create a
Capture.

sub b { .Capture };
put b.perl;
# OUTPUT: «\("a", "b", "c")␤»

HTH, Bill.

PS...While I've been able to find a reference to the code
you showed with two angle brackets (quoting construct):

https://docs.perl6.org/syntax/%3C%3C%20%3E%3E

...I haven't been able to find an explicit doc reference to the
"two-angle-bracket" quoting construct surrounding (among other things)
a block.







On Tue, Sep 3, 2019 at 11:41 AM The Sidhekin  wrote:
>
> On Tue, Sep 3, 2019 at 8:18 PM William Michels  wrote:
>>
>> PS Eirik, I think people might be referring to <{...}> as "pointy
>> blocks", but I'm really not sure... .
>
>
>   I'm pretty sure Perl6 pointy blocks still refer to block constructors with 
> signatures, like: C<< my $add = -> $a, $b = 2 { $a + $b }; >>
>
>   Oh hey, there's an index for it: 
> https://docs.perl6.org/language/functions#index-entry-pointy_blocks
>
>   (The indexed docs don't actually define the term though …)
>
>
> Eirik


Re: Variable character class

2019-09-03 Thread The Sidhekin
On Tue, Sep 3, 2019 at 8:18 PM William Michels 
wrote:

> PS Eirik, I think people might be referring to <{...}> as "pointy
> blocks", but I'm really not sure... .
>

  I'm pretty sure Perl6 pointy blocks still refer to block constructors
with signatures, like: C<< my $add = -> $a, $b = 2 { $a + $b }; >>

  Oh hey, there's an index for it:
https://docs.perl6.org/language/functions#index-entry-pointy_blocks

  (The indexed docs don't actually define the term though …)


Eirik


Re: Variable character class

2019-09-03 Thread William Michels via perl6-users
Someone might get a kick out of this ;-). Clearly regexes are built on
top of set theory, but as both Simon and Yary pointed out, my
set-based code didn't return the matching string "8420" present in the
target.

Example A, Eirik's code used an array to generate a character class,
and then tested that character class in a regex vs the target ($_) .
In Example B, all it takes is the addition of the "frugal" match
indicator ("+?") to Eirik's code (and presumably to Simon's code) to
give almost identical results as a set-intesection.

The results aren't completely identical because the regex code
(Example A, Example B) is non-symmetric.  As shown below ("lorem
ipsum"), when Example B (array regex) is compared to Example C (set
intersection), multiple copies of character class elements present in
the "target" string show up in the regex match object (Example B),
while these duplicate elements are eliminated from the "symmetrical"
set-intersection result (Example C):

##_A
sub contains( Str $chars, Str $_ ) {
  my @arr = $chars.comb.unique;
  return m:g/@arr+/
}

say contains("24680", "19584203").join("|"); # says 8420
say contains("19584203", "24680").join("|"); # says 24|80
say contains("Lorem ipsum dolor sit amet, consectetuer adipiscing
elit.", "abcdefg").join("|"); # says a|cde|g
say contains("abcdefg", "Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.").join("|"); # says e|d|a|e|c|ec|e|e|ad|c|g|e

##_B
sub nonsym_intersect( Str $chars, Str $_ ) {
  my @arr = $chars.comb.unique;
  return m:g/@arr+?/
}

say nonsym_intersect("24680", "19584203").join("|"); # says 8|4|2|0
say nonsym_intersect("19584203", "24680").join("|"); # says 2|4|8|0
say nonsym_intersect("Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.", "abcdefg").join("|"); # says a|c|d|e|g
say nonsym_intersect("abcdefg", "Lorem ipsum dolor sit amet,
consectetuer adipiscing elit.").join("|"); # says
e|d|a|e|c|e|c|e|e|a|d|c|g|e

##_C
sub sym_intersect(Str $a, Str $b) {
   my @c = $a.comb.unique;
   my @d = $b.comb.unique;
   #return (~[@c (&) @d]).^name;
   return ~[@c (&) @d];
}

say sym_intersect("24680", "19584203").words.join("|"); # says 2|8|4|0
say sym_intersect("19584203", "24680").words.join("|"); # says 8|4|2|0
say sym_intersect("Lorem ipsum dolor sit amet, consectetuer adipiscing
elit.", "abcdefg").words.join("|"); # says  a|g|c|d|e
say sym_intersect("abcdefg", "Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.").words.join("|"); # says a|d|e|g|c


One caveat (above, Example C), I can't return from a set-intersection
and just do a "join" on the result, as in the previous two examples. I
have to break the return into ".words" and then ".join", to match the
format of the previous two examples.

HTH, Bill.

PS Eirik, I think people might be referring to <{...}> as "pointy
blocks", but I'm really not sure... .





On Mon, Sep 2, 2019 at 11:25 AM Joseph Brenner  wrote:
>
> >   The "implicit" alternation comes from interpolating a list (of subrules,
> > see below).
>
> I see.  And that's discussed here (had to really look for it):
>
> https://docs.perl6.org/language/regexes#Quoted_lists_are_LTM_matches
>
> At first I was looking further down in the "Regex interpolation"
> section, where it's also touched on, though I kept missing it:
>
> > When an array variable is interpolated into a regex, the regex engine 
> > handles it like a | alternative of the regex elements (see the 
> > documentation on embedded lists, above).
>
>
> On 9/1/19, The Sidhekin  wrote:
> > On Mon, Sep 2, 2019 at 1:12 AM Joseph Brenner  wrote:
> >
> >> I was just trying to run Simon Proctor's solution, and I see it
> >> working for Yary's first case, but not his more complex one with
> >> problem characters like brackets included in the list of characters.
> >>
> >> I don't really see how to fix it, in part because I'm not that
> >> clear on what it's actually doing... there's some sort of
> >> implicit alternation going on?
> >>
> >>
> >> sub contains( Str $chars, Str $_ ) {
> >>   m:g/<{$chars.comb}>+/
> >> };
> >>
> >
> >   The "implicit" alternation comes from interpolating a list (of subrules,
> > see below).
> >
> > That works for this case:
> >>
> >>   say contains('24680', '19584203');
> >>   # (「8420」)
> >>
> >> But on something like this it errors out:
> >>
> >>   say contains('+\/\]\[', 'Apple ][+//e'); # says ][+//
> >>
> >
> >   … because it's trying to compile each (1-character) string as a subrule …
> >
> >   To have the (1-character) strings used a literals, rather than compiled
> > as subrules, put them in an array instead of a block wrapped in angle
> > brackets:
> >
> > sub contains( Str $chars, Str $_ ) {
> >   my @arr = $chars.comb;
> >   m:g/@arr+/
> > }
> >
> >
> >   (… hey, is there a word for "block wrapped in angle brackets"?)
> >
> >
> > Eirik
> >


Re: Variable character class

2019-09-03 Thread Gianni Ceccarelli
On Tue, 3 Sep 2019 09:15:54 -0700
William Michels via perl6-users  wrote:

> Just a short note that Eirik's array-based code seems to work fine,
> with-or-without backslash-escaping the first input string (minimal
> testing, below):

Oh, sure. But when the target string contains backslashes, it will
behave differently from the P5 version, in that the P5 version won't
report the backslashes as matching (because of the way it builds its
regex).


-- 
Dakkar - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88


Re: Variable character class

2019-09-03 Thread William Michels via perl6-users
Hi Gianni,

Thank you for demonstrating use of the "Test" module in your code.
Just a short note that Eirik's array-based code seems to work fine,
with-or-without backslash-escaping the first input string (minimal
testing, below):

sub contains( Str $chars, Str $_ ) {
  my @arr = $chars.comb;
  m:g/@arr+/
}

say contains('+\/\]\[', 'Apple ][+//e'); # returns (「][+//」)
say contains('+/][', 'Apple ][+//e'); # returns (「][+//」)

HTH, Bill.




On Mon, Sep 2, 2019 at 10:15 AM Gianni Ceccarelli
 wrote:
>
> On 2019-09-02 The Sidhekin  wrote:
> >   To have the (1-character) strings used a literals, rather than
> > compiled as subrules, put them in an array instead of a block wrapped
> > in angle brackets:
> >
> > sub contains( Str $chars, Str $_ ) {
> >   my @arr = $chars.comb;
> >   m:g/@arr+/
>
> This looks to be the correct solution. Notice that yary's initial code
> had::
>
>   matching_chars('+\/\]\[', 'Apple ][+//e')
>
> but those backslashes are an implementation artefact (the Perl5
> version interpolates the given string into a regex without calling
> ``quotemeta``).
>
> If we remove the backslashes, Sidhekin's code works::
>
>   #!perl6
>   use v6;
>   use Test;
>
>   sub matching_chars(Str $chars_to_match, Str $target) {
> my @chars-to-match = $chars_to_match.comb;
> return $target ~~ m:g/@chars-to-match + /;
>   }
>
>   is matching_chars("24680", "19584203"),['8420'];
>   is matching_chars("Lorem ipsum dolor sit amet, consectetueradipiscing 
> elit.",
> "abcdef"), ['a','cde'];
>   is matching_chars('+/][', 'Apple ][+//e'),['][+//'];
>   is matching_chars('24680', '19584203'),['8420'];
>   is matching_chars('24680', '24680x'),['24680'];
>   is matching_chars('246','13 91 1462 7'),['462'];
>
>   done-testing;
>
> --
> Dakkar - 
> GPG public key fingerprint = A071 E618 DD2C 5901 9574
>  6FE2 40EA 9883 7519 3F88
> key id = 0x75193F88
>
> Just because it's not nice doesn't mean it's not miraculous.
> -- (Terry Pratchett, Interesting Times)


Re: Variable character class

2019-09-02 Thread Joseph Brenner
>   The "implicit" alternation comes from interpolating a list (of subrules,
> see below).

I see.  And that's discussed here (had to really look for it):

https://docs.perl6.org/language/regexes#Quoted_lists_are_LTM_matches

At first I was looking further down in the "Regex interpolation"
section, where it's also touched on, though I kept missing it:

> When an array variable is interpolated into a regex, the regex engine handles 
> it like a | alternative of the regex elements (see the documentation on 
> embedded lists, above).


On 9/1/19, The Sidhekin  wrote:
> On Mon, Sep 2, 2019 at 1:12 AM Joseph Brenner  wrote:
>
>> I was just trying to run Simon Proctor's solution, and I see it
>> working for Yary's first case, but not his more complex one with
>> problem characters like brackets included in the list of characters.
>>
>> I don't really see how to fix it, in part because I'm not that
>> clear on what it's actually doing... there's some sort of
>> implicit alternation going on?
>>
>>
>> sub contains( Str $chars, Str $_ ) {
>>   m:g/<{$chars.comb}>+/
>> };
>>
>
>   The "implicit" alternation comes from interpolating a list (of subrules,
> see below).
>
> That works for this case:
>>
>>   say contains('24680', '19584203');
>>   # (「8420」)
>>
>> But on something like this it errors out:
>>
>>   say contains('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>
>
>   … because it's trying to compile each (1-character) string as a subrule …
>
>   To have the (1-character) strings used a literals, rather than compiled
> as subrules, put them in an array instead of a block wrapped in angle
> brackets:
>
> sub contains( Str $chars, Str $_ ) {
>   my @arr = $chars.comb;
>   m:g/@arr+/
> }
>
>
>   (… hey, is there a word for "block wrapped in angle brackets"?)
>
>
> Eirik
>


Re: Variable character class

2019-09-02 Thread Gianni Ceccarelli
On 2019-09-02 The Sidhekin  wrote:
>   To have the (1-character) strings used a literals, rather than
> compiled as subrules, put them in an array instead of a block wrapped
> in angle brackets:
> 
> sub contains( Str $chars, Str $_ ) {
>   my @arr = $chars.comb;
>   m:g/@arr+/

This looks to be the correct solution. Notice that yary's initial code
had::

  matching_chars('+\/\]\[', 'Apple ][+//e')

but those backslashes are an implementation artefact (the Perl5
version interpolates the given string into a regex without calling
``quotemeta``).

If we remove the backslashes, Sidhekin's code works::

  #!perl6
  use v6;
  use Test;

  sub matching_chars(Str $chars_to_match, Str $target) {
my @chars-to-match = $chars_to_match.comb;
return $target ~~ m:g/@chars-to-match + /;
  }

  is matching_chars("24680", "19584203"),['8420'];
  is matching_chars("Lorem ipsum dolor sit amet, consectetueradipiscing elit.",
"abcdef"), ['a','cde'];
  is matching_chars('+/][', 'Apple ][+//e'),['][+//'];
  is matching_chars('24680', '19584203'),['8420'];
  is matching_chars('24680', '24680x'),['24680'];
  is matching_chars('246','13 91 1462 7'),['462'];

  done-testing;

-- 
Dakkar - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88

Just because it's not nice doesn't mean it's not miraculous.
-- (Terry Pratchett, Interesting Times)


Re: Variable character class

2019-09-02 Thread Paul Procacci
I don't know then.

I've created the following ticket:

https://github.com/perl6/doc/issues/2999

Feel free to place your own input there if you feel the need.

On Mon, Sep 2, 2019 at 12:37 PM William Michels 
wrote:

> Sorry Paul, I don't get the correct answer in any of the three cases I
> tried. Here's what 6Pad returns:
>
> https://perl6.github.io/6pad/
>
> sub matching_chars(Str $chars_to_match, Str $str) {
> # warnings, treats as string not variable
> $str ~~ /<$_>/ given "<[$chars_to_match]>";
> }
>
> say matching_chars("24680", "19584203"); # expect 「8420」
> say matching_chars("Lorem ipsum dolor sit amet, consectetuer
> adipiscing elit.", "abcdef"); # expect 「a」 「cde」
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # expect 「][+//」
>
> 「19584203」
> 「abcdef」
> ===SORRY!=== Error while compiling ?/EVAL_7 Malformed regex at
> ?/EVAL_7:1 --> anon regex { Apple ][+//e} expecting any of: infix
> stopper
>
> HTH, Bill.
>
>
> On Mon, Sep 2, 2019 at 7:54 AM Paul Procacci  wrote:
> >
> > Was talking to folks over on the #perl6 IRC channel.
> > It appears the recommended way is:
> >
> > sub matching_chars(Str $chars_to_match, Str $str) {
> > # warnings, treats as string not variable
> > $str ~~ /<$_>/ given "<[$chars_to_match]>";
> > }
> >
> > ~Paul
> >
> >
> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
> >>
> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
> specifying a character class via a variable.
> >>
> >> Perl 5:
> >> sub matching_chars {
> >>   (my $chars_to_match, local $_) = @_;
> >>   /([$chars_to_match]+)/
> >> }
> >>
> >> say matching_chars('24680', '19584203'); # says 8420
> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
> >>
> >> Perl 6:
> >> sub matching_chars(Str $chars_to_match, Str $_) {
> >> # warnings, treats as string not variable
> >> m/<[$chars_to_match]>/;
> >> }
> >>
> >> How do I get Perl 6 to interpret a variable in the contents of a
> character class?
> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
> think that  Rakudo would use the literal contents of $chars_to_match,
> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
> warning about repeated c, underscore, etc.
> >>
> >> -y
> >
> >
> >
> > --
> > __
> >
> > :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Variable character class

2019-09-02 Thread William Michels via perl6-users
Sorry Paul, I don't get the correct answer in any of the three cases I
tried. Here's what 6Pad returns:

https://perl6.github.io/6pad/

sub matching_chars(Str $chars_to_match, Str $str) {
# warnings, treats as string not variable
$str ~~ /<$_>/ given "<[$chars_to_match]>";
}

say matching_chars("24680", "19584203"); # expect 「8420」
say matching_chars("Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.", "abcdef"); # expect 「a」 「cde」
say matching_chars('+\/\]\[', 'Apple ][+//e'); # expect 「][+//」

「19584203」
「abcdef」
===SORRY!=== Error while compiling ?/EVAL_7 Malformed regex at
?/EVAL_7:1 --> anon regex { Apple ][+//e} expecting any of: infix
stopper

HTH, Bill.


On Mon, Sep 2, 2019 at 7:54 AM Paul Procacci  wrote:
>
> Was talking to folks over on the #perl6 IRC channel.
> It appears the recommended way is:
>
> sub matching_chars(Str $chars_to_match, Str $str) {
> # warnings, treats as string not variable
> $str ~~ /<$_>/ given "<[$chars_to_match]>";
> }
>
> ~Paul
>
>
> On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>>
>> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying a 
>> character class via a variable.
>>
>> Perl 5:
>> sub matching_chars {
>>   (my $chars_to_match, local $_) = @_;
>>   /([$chars_to_match]+)/
>> }
>>
>> say matching_chars('24680', '19584203'); # says 8420
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>
>> Perl 6:
>> sub matching_chars(Str $chars_to_match, Str $_) {
>> # warnings, treats as string not variable
>> m/<[$chars_to_match]>/;
>> }
>>
>> How do I get Perl 6 to interpret a variable in the contents of a character 
>> class?
>> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think 
>> that  Rakudo would use the literal contents of $chars_to_match, instead it's 
>> using the literal chars "$ c h a r s _ t o _ m a t c h" and warning about 
>> repeated c, underscore, etc.
>>
>> -y
>
>
>
> --
> __
>
> :(){ :|:& };:


Re: Variable character class

2019-09-02 Thread Paul Procacci
Was talking to folks over on the #perl6 IRC channel.
It appears the recommended way is:

sub matching_chars(Str $chars_to_match, Str $str) {
# warnings, treats as string not variable
$str ~~ /<$_>/ given "<[$chars_to_match]>";
}

~Paul


On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:

> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying
> a character class via a variable.
>
> Perl 5:
> sub matching_chars {
>   (my $chars_to_match, local $_) = @_;
>   /([$chars_to_match]+)/
> }
>
> say matching_chars('24680', '19584203'); # says 8420
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>
> Perl 6:
> sub matching_chars(Str $chars_to_match, Str $_) {
> # warnings, treats as string not variable
> m/<[$chars_to_match]>/;
> }
>
> How do I get Perl 6 to interpret a variable in the contents of a character
> class?
> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think
> that  Rakudo would use the literal contents of $chars_to_match, instead
> it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning
> about repeated c, underscore, etc.
>
> -y
>


-- 
__

:(){ :|:& };:


Re: Variable character class

2019-09-02 Thread William Michels via perl6-users
Hi Aureliano!

Others will certainly have a more informed answer, but I think it
might be because:

1). Set operations in general in Perl6 (on characters, etc.) may use a
unordered matching algorithm to enhance speed, and/or
2). I didn't explicitly call a "Sort" method, function or routine in
matching the characters, and/or
3). The resulting character order (probably represented internally as
Unicode codepoints), may indeed have some sort of order to them, but
that order/pseudo-order is either A. related to their initial position
in the input string (0,1,2,3...), or B. that order/pseudo-order is
related to each character's numeric representation in Unicode.

Below I convert the code to accept two integers instead of two
strings. I set up a few different "return" lines for you to check out.
You will see that the (second) "return" line that calls ".sort" as the
last method indeed returns a set of integers in numerical order! But
it also looks as though the (first) return line without an explicit
".sort" call ALSO returns the Intersection Set in numerical order,
"(set(0 2 4 6 8))".

my Int $u = 24680;
my @s = $u.comb.unique;
say @s ; # [2 4 6 8 0]

sub matching_chars(Int $a, Int $b) {
   my @c = $a.comb.unique;
   my @d = $b.comb.unique;
   #return @c (&) @d; # Returns Intersection Set (Ints)
   #return [@c (&) @d].sort; # Returns Sorted Set (Ints)
   return [@c (&) @d].sort.Str; # Returns varying order (Strs)
}

say matching_chars(24680, 1234567890); # RETURNS (set(0 2 4 6 8))


So I think Perl6 may have fast integer matching/sorting algorithms
built-in, but the built-in matching/sorting algorithms for characters
may be somewhat different, different enough to require a
explicit/final (e.g. alphabetic) sort, if that's what the programmer
desires.

HTH, Bill.






On Sun, Sep 1, 2019 at 10:10 PM Aureliano Guedes
 wrote:
>
> Why does it dont return ordered for neither of those lists?
> I mean:
>
> my $u = "24680";
> my @s = $u.comb.unique;
> say @s ; # [2 4 6 8 0]
>
> sub matching_chars(Str $a, Str $b) {
>my @c = $a.comb.unique;
>my @d = $b.comb.unique;
>return ~[@c (&) @d];
> }
>
> say matching_chars("24680", "1234567890"); # says 2 0 8 4
>
> On Mon, Sep 2, 2019 at 1:20 AM William Michels via perl6-users 
>  wrote:
>>
>> Thanks Simon, good point. I ran into the same trouble as others trying
>> to get the answer via regex, and switched over to sets as an
>> alternative. I'll confess I completely missed that Yary's Perl5 code
>> returned the substring "8420" present in his test "19584203" string,
>> and that was the answer he was going for.
>>
>> Simon and Eirik, I still think there might be some value in calling
>> the .unique method on $chars.comb as in "$chars.comb.unique". But I
>> haven't had the opportunity to check code efficiency  +/-  the
>> ".unique" method call.
>>
>> For those interested in using Sets as a solution, you can stringifying
>> the 'return' line (below). Still working on using "join()" to
>> concatenate!
>>
>> sub matching_chars(Str $a, Str $b) {
>>my @c = $a.comb.unique;
>>my @d = $b.comb.unique;
>>#return (~[@c (&) @d]).^name;
>>return ~[@c (&) @d];
>> }
>>
>> say matching_chars("24680", "19584203"); # says 2 0 8 4
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says [ + ] /
>> say matching_chars("Lorem ipsum dolor sit amet, consectetuer
>> adipiscing elit.", "abcdef"); # says c a d e
>>
>>
>> HTH, Bill.
>>
>> PS. Simon, thanks for all the great Youtube videos you've done from LondonPM!
>>
>> https://youtu.be/yt8SrZ_V_50
>> https://youtu.be/9M1xZQ0_Skw
>>
>>
>>
>>
>>
>>
>>
>>
>> On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor  wrote:
>> >
>> > Using a set would be good but it doesn't give you the matching string from 
>> > the original (which is what I thought was required) otherwise Sets would 
>> > be my first thought.
>> >
>> > On Sun, 1 Sep 2019, 17:57 William Michels,  wrote:
>> >>
>> >> Hi Yary and Paul and Simon,
>> >>
>> >> I ran into the same difficulties as Yary with repeated characters, so
>> >> I tried the .unique method. Then after a while, I realized that
>> >> problems like this might best be treated as "Set" problems in Perl6.
>> >> Note the Set Intersection operator "(&)" below:
>> >>
>> >> sub matching_chars(Str $a, Str $b) {
>> >>my @c = $a.comb.unique;
>> >>my @d = $b.comb.unique;
>> >>#say @c; say @d;
>> >>return @c (&) @d;
>> >> }
>> >>
>> >> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
>> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>> >>
>> >>
>> >> https://docs.perl6.org/routine/Set
>> >> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>> >>
>> >> HTH, Bill.
>> >>
>> >>
>> >> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci  wrote:
>> >> >
>> >> > I'm not entirely sure if this is the correct answer, but if you define 
>> >> > your own custom character class
>> >> > with a 'regex' object, you can use that in the grouping.
>> >> >
>> >> > sub matching_chars(Str 

Re: Variable character class

2019-09-01 Thread William Michels via perl6-users
Thanks Simon, good point. I ran into the same trouble as others trying
to get the answer via regex, and switched over to sets as an
alternative. I'll confess I completely missed that Yary's Perl5 code
returned the substring "8420" present in his test "19584203" string,
and that was the answer he was going for.

Simon and Eirik, I still think there might be some value in calling
the .unique method on $chars.comb as in "$chars.comb.unique". But I
haven't had the opportunity to check code efficiency  +/-  the
".unique" method call.

For those interested in using Sets as a solution, you can stringifying
the 'return' line (below). Still working on using "join()" to
concatenate!

sub matching_chars(Str $a, Str $b) {
   my @c = $a.comb.unique;
   my @d = $b.comb.unique;
   #return (~[@c (&) @d]).^name;
   return ~[@c (&) @d];
}

say matching_chars("24680", "19584203"); # says 2 0 8 4
say matching_chars('+\/\]\[', 'Apple ][+//e'); # says [ + ] /
say matching_chars("Lorem ipsum dolor sit amet, consectetuer
adipiscing elit.", "abcdef"); # says c a d e


HTH, Bill.

PS. Simon, thanks for all the great Youtube videos you've done from LondonPM!

https://youtu.be/yt8SrZ_V_50
https://youtu.be/9M1xZQ0_Skw








On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor  wrote:
>
> Using a set would be good but it doesn't give you the matching string from 
> the original (which is what I thought was required) otherwise Sets would be 
> my first thought.
>
> On Sun, 1 Sep 2019, 17:57 William Michels,  wrote:
>>
>> Hi Yary and Paul and Simon,
>>
>> I ran into the same difficulties as Yary with repeated characters, so
>> I tried the .unique method. Then after a while, I realized that
>> problems like this might best be treated as "Set" problems in Perl6.
>> Note the Set Intersection operator "(&)" below:
>>
>> sub matching_chars(Str $a, Str $b) {
>>my @c = $a.comb.unique;
>>my @d = $b.comb.unique;
>>#say @c; say @d;
>>return @c (&) @d;
>> }
>>
>> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>>
>>
>> https://docs.perl6.org/routine/Set
>> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>>
>> HTH, Bill.
>>
>>
>> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci  wrote:
>> >
>> > I'm not entirely sure if this is the correct answer, but if you define 
>> > your own custom character class
>> > with a 'regex' object, you can use that in the grouping.
>> >
>> > sub matching_chars(Str $chars_to_match, Str $_) {
>> > my regex x { $chars_to_match ** 1 };
>> > m/<[]>/;
>> > }
>> >
>> > The above worked for me in the very small testing I did.
>> >
>> > ~Paul
>> >
>> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>> >>
>> >> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying 
>> >> a character class via a variable.
>> >>
>> >> Perl 5:
>> >> sub matching_chars {
>> >>   (my $chars_to_match, local $_) = @_;
>> >>   /([$chars_to_match]+)/
>> >> }
>> >>
>> >> say matching_chars('24680', '19584203'); # says 8420
>> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>> >>
>> >> Perl 6:
>> >> sub matching_chars(Str $chars_to_match, Str $_) {
>> >> # warnings, treats as string not variable
>> >> m/<[$chars_to_match]>/;
>> >> }
>> >>
>> >> How do I get Perl 6 to interpret a variable in the contents of a 
>> >> character class?
>> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think 
>> >> that  Rakudo would use the literal contents of $chars_to_match, instead 
>> >> it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning 
>> >> about repeated c, underscore, etc.
>> >>
>> >> -y
>> >
>> >
>> >
>> > --
>> > __
>> >
>> > :(){ :|:& };:


Re: Variable character class

2019-09-01 Thread The Sidhekin
On Mon, Sep 2, 2019 at 1:12 AM Joseph Brenner  wrote:

> I was just trying to run Simon Proctor's solution, and I see it
> working for Yary's first case, but not his more complex one with
> problem characters like brackets included in the list of characters.
>
> I don't really see how to fix it, in part because I'm not that
> clear on what it's actually doing... there's some sort of
> implicit alternation going on?
>
>
> sub contains( Str $chars, Str $_ ) {
>   m:g/<{$chars.comb}>+/
> };
>

  The "implicit" alternation comes from interpolating a list (of subrules,
see below).

That works for this case:
>
>   say contains('24680', '19584203');
>   # (「8420」)
>
> But on something like this it errors out:
>
>   say contains('+\/\]\[', 'Apple ][+//e'); # says ][+//
>

  … because it's trying to compile each (1-character) string as a subrule …

  To have the (1-character) strings used a literals, rather than compiled
as subrules, put them in an array instead of a block wrapped in angle
brackets:

sub contains( Str $chars, Str $_ ) {
  my @arr = $chars.comb;
  m:g/@arr+/
}


  (… hey, is there a word for "block wrapped in angle brackets"?)


Eirik


Re: Variable character class

2019-09-01 Thread Joseph Brenner
I was just trying to run Simon Proctor's solution, and I see it
working for Yary's first case, but not his more complex one with
problem characters like brackets included in the list of characters.

I don't really see how to fix it, in part because I'm not that
clear on what it's actually doing... there's some sort of
implicit alternation going on?


sub contains( Str $chars, Str $_ ) {
  m:g/<{$chars.comb}>+/
};

That works for this case:

  say contains('24680', '19584203');
  # (「8420」)

But on something like this it errors out:

  say contains('+\/\]\[', 'Apple ][+//e'); # says ][+//

# ===SORRY!=== Error while compiling
/home/doom/End/Cave/Perl6/bin/trial-yary_building_char_class_from_array_chars.pl6
# Bogus statement
# at 
/home/doom/End/Cave/Perl6/bin/trial-yary_building_char_class_from_array_chars.pl6:33
# --> say contains('+\/\]\[', 'Apple ][+//e');⏏ # says ][+//
# expecting any of:
# prefix
# term




On 9/1/19, Simon Proctor  wrote:
> sub contains ( Str $chars, Str $_ ) {
>   m:g/<{$chars.comb}>+/
> };
>
> This will return all the sets of matching strings but it is doing runtime
> evaluation of your character string so it's a bit slow.
>
> On Sun, 1 Sep 2019 at 04:59, Paul Procacci  wrote:
>
>> I'm not entirely sure if this is the correct answer, but if you define
>> your own custom character class
>> with a 'regex' object, you can use that in the grouping.
>>
>> sub matching_chars(Str $chars_to_match, Str $_) {
>> my regex x { $chars_to_match ** 1 };
>> m/<[]>/;
>> }
>>
>> The above worked for me in the very small testing I did.
>>
>> ~Paul
>>
>> On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>>
>>> I found something easy in Perl 5 that's puzzling me in Perl 6-
>>> specifying
>>> a character class via a variable.
>>>
>>> Perl 5:
>>> sub matching_chars {
>>>   (my $chars_to_match, local $_) = @_;
>>>   /([$chars_to_match]+)/
>>> }
>>>
>>> say matching_chars('24680', '19584203'); # says 8420
>>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>>
>>> Perl 6:
>>> sub matching_chars(Str $chars_to_match, Str $_) {
>>> # warnings, treats as string not variable
>>> m/<[$chars_to_match]>/;
>>> }
>>>
>>> How do I get Perl 6 to interpret a variable in the contents of a
>>> character class?
>>> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
>>> think that  Rakudo would use the literal contents of $chars_to_match,
>>> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
>>> warning about repeated c, underscore, etc.
>>>
>>> -y
>>>
>>
>>
>> --
>> __
>>
>> :(){ :|:& };:
>>
>
>
> --
> Simon Proctor
> Cognoscite aliquid novum cotidie
>
> http://www.khanate.co.uk/
>


Re: Variable character class

2019-09-01 Thread yary
Hi Paul,

Neither of those match for me- adapted below

#!/usr/bin/env perl6
use v6;

sub matching_chars(Str $match, Str $y) {
  say 'eval  = ', $y.match: /<{ "<[$match]>" }>/;
say 'direct= ', $y.match: /<[$match]>/;

my $rematch := $match;
say 'bound = ', $y.match: /<[$rematch]>/;

  say 'orig $match=',$match.perl, ' vs $rematch=',$rematch.perl;
}
matching_chars('246','13 91 1462 7');

Prints
*eval  = 「4」*
*direct= Nil*
*bound = Nil*
*orig $match="246" vs $rematch="246"*

I'd expect working match to say '4' as the first matching character from
'246', instead your example gives me Nil with both. Can you post the full
code that has one working, other non-working?

-y


On Sun, Sep 1, 2019 at 12:53 PM Paul Procacci  wrote:

> I've actually found some weird inconsistancy while playing with this.
>
> sub matching_chars(Str $x, Str $y) {
> my $a := $x;
> my $b := $y;
> say $y.match: /<[$a]>/;
> }
>
> That results in a match of one character, yet:
>
> sub matching_chars(Str $x, Str $y) {
> my $a := $x;
> my $b := $y;
> say $y.match: /<[$x]>/;
> }
>
> Results in a match of none of the characters.  Yet, $x and $a should
> reference the same object.
> Something is broken.
>
> On Sun, Sep 1, 2019 at 2:39 PM yary  wrote:
>
>> Thanks for the ideas. The core issue I'm probing is runtime construction
>> of character classes, with an eye towards opening a documentation issue, or
>> maybe even an issue against the character class implementation.
>>
>> Simon's workaround m:g/<{$chars.comb}>+/  is interesting, interpolating
>> a list which turns into alternation - but it isn't a character class.
>> Bill's proposal with intersecting sets - via a junction, though ∩ aka (&)
>> also ought to work - is short and clear, but solves a different problem.
>>
>> Paul's suggestion should create a character class, but my test doesn't
>> work:
>>
>> #!/usr/bin/env perl6
>> use v6;
>>
>> sub matching_chars(Str $chars_to_match, Str $_) {
>> print "Looking for $chars_to_match in '$_'- ";
>> my regex x { $chars_to_match ** 1 };
>> m/<[]>/;
>> }
>> say matching_chars('24680', '19584203');
>> say matching_chars('24680', '24680x');
>>
>> $ ./*match.p6*
>> Looking for 24680 in '19584203'- Nil
>> Looking for 24680 in '24680x'- 「x」
>>
>> Paul, can you post a more complete working test?
>>
>> The below works but is "ugly", building up the character class syntax as
>> a string and then evaluating it
>>
>> sub matching_chars(Str $chars_to_match, Str $_) {
>>   print "Looking for $chars_to_match in '$_'- ";
>>
>> m/<{ # Interpolate contents as a regex
>>  "<[" ~ # Open the character class
>> # Add the char list, with closing bracket backslashed
>> $chars_to_match.subst(']','\]' ,:g) ~
>> "]>" # Close the character class
>>   }>+/; # End interpolation, plus sign for one-or-more
>> }
>> say matching_chars('24680', '19584203'); # matches 8420
>> say matching_chars('+/][', 'Apple ][+//e'); # matches ][+//
>>
>> I see this as a shortcoming in the character class implementation.
>> Specifically the "literal" vs "metacharacter" distinction
>> 
>> goes out the window with enumerated character classes, it behaves like a
>> single-quoting. I want all the different interpolation goodness
>>  to work
>> with character classes, and it seems that requires a new syntax.
>>
>> -y
>>
>>
>> On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor 
>> wrote:
>>
>>> Using a set would be good but it doesn't give you the matching string
>>> from the original (which is what I thought was required) otherwise Sets
>>> would be my first thought.
>>>
>>> On Sun, 1 Sep 2019, 17:57 William Michels, 
>>> wrote:
>>>
 Hi Yary and Paul and Simon,

 I ran into the same difficulties as Yary with repeated characters, so
 I tried the .unique method. Then after a while, I realized that
 problems like this might best be treated as "Set" problems in Perl6.
 Note the Set Intersection operator "(&)" below:

 sub matching_chars(Str $a, Str $b) {
my @c = $a.comb.unique;
my @d = $b.comb.unique;
#say @c; say @d;
return @c (&) @d;
 }

 say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
 say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])


 https://docs.perl6.org/routine/Set
 https://docs.perl6.org/language/operators#infix_(&),_infix_∩

 HTH, Bill.


 On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci 
 wrote:
 >
 > I'm not entirely sure if this is the correct answer, but if you
 define your own custom character class
 > with a 'regex' object, you can use that in the grouping.
 >
 > sub matching_chars(Str $chars_to_match, Str $_) {
 > my regex x { $chars_to_match ** 1 };
 > m/<[]>/;
 > }
 >
 > The above worked for me in the 

Re: Variable character class

2019-09-01 Thread Paul Procacci
I've actually found some weird inconsistancy while playing with this.

sub matching_chars(Str $x, Str $y) {
my $a := $x;
my $b := $y;
say $y.match: /<[$a]>/;
}

That results in a match of one character, yet:

sub matching_chars(Str $x, Str $y) {
my $a := $x;
my $b := $y;
say $y.match: /<[$x]>/;
}

Results in a match of none of the characters.  Yet, $x and $a should
reference the same object.
Something is broken.

On Sun, Sep 1, 2019 at 2:39 PM yary  wrote:

> Thanks for the ideas. The core issue I'm probing is runtime construction
> of character classes, with an eye towards opening a documentation issue, or
> maybe even an issue against the character class implementation.
>
> Simon's workaround m:g/<{$chars.comb}>+/  is interesting, interpolating a
> list which turns into alternation - but it isn't a character class. Bill's
> proposal with intersecting sets - via a junction, though ∩ aka (&) also
> ought to work - is short and clear, but solves a different problem.
>
> Paul's suggestion should create a character class, but my test doesn't
> work:
>
> #!/usr/bin/env perl6
> use v6;
>
> sub matching_chars(Str $chars_to_match, Str $_) {
> print "Looking for $chars_to_match in '$_'- ";
> my regex x { $chars_to_match ** 1 };
> m/<[]>/;
> }
> say matching_chars('24680', '19584203');
> say matching_chars('24680', '24680x');
>
> $ ./*match.p6*
> Looking for 24680 in '19584203'- Nil
> Looking for 24680 in '24680x'- 「x」
>
> Paul, can you post a more complete working test?
>
> The below works but is "ugly", building up the character class syntax as a
> string and then evaluating it
>
> sub matching_chars(Str $chars_to_match, Str $_) {
>   print "Looking for $chars_to_match in '$_'- ";
>
> m/<{ # Interpolate contents as a regex
>  "<[" ~ # Open the character class
> # Add the char list, with closing bracket backslashed
> $chars_to_match.subst(']','\]' ,:g) ~
> "]>" # Close the character class
>   }>+/; # End interpolation, plus sign for one-or-more
> }
> say matching_chars('24680', '19584203'); # matches 8420
> say matching_chars('+/][', 'Apple ][+//e'); # matches ][+//
>
> I see this as a shortcoming in the character class implementation.
> Specifically the "literal" vs "metacharacter" distinction
>  goes
> out the window with enumerated character classes, it behaves like a
> single-quoting. I want all the different interpolation goodness
>  to work with
> character classes, and it seems that requires a new syntax.
>
> -y
>
>
> On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor 
> wrote:
>
>> Using a set would be good but it doesn't give you the matching string
>> from the original (which is what I thought was required) otherwise Sets
>> would be my first thought.
>>
>> On Sun, 1 Sep 2019, 17:57 William Michels,  wrote:
>>
>>> Hi Yary and Paul and Simon,
>>>
>>> I ran into the same difficulties as Yary with repeated characters, so
>>> I tried the .unique method. Then after a while, I realized that
>>> problems like this might best be treated as "Set" problems in Perl6.
>>> Note the Set Intersection operator "(&)" below:
>>>
>>> sub matching_chars(Str $a, Str $b) {
>>>my @c = $a.comb.unique;
>>>my @d = $b.comb.unique;
>>>#say @c; say @d;
>>>return @c (&) @d;
>>> }
>>>
>>> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
>>> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>>>
>>>
>>> https://docs.perl6.org/routine/Set
>>> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>>>
>>> HTH, Bill.
>>>
>>>
>>> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci 
>>> wrote:
>>> >
>>> > I'm not entirely sure if this is the correct answer, but if you define
>>> your own custom character class
>>> > with a 'regex' object, you can use that in the grouping.
>>> >
>>> > sub matching_chars(Str $chars_to_match, Str $_) {
>>> > my regex x { $chars_to_match ** 1 };
>>> > m/<[]>/;
>>> > }
>>> >
>>> > The above worked for me in the very small testing I did.
>>> >
>>> > ~Paul
>>> >
>>> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>>> >>
>>> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
>>> specifying a character class via a variable.
>>> >>
>>> >> Perl 5:
>>> >> sub matching_chars {
>>> >>   (my $chars_to_match, local $_) = @_;
>>> >>   /([$chars_to_match]+)/
>>> >> }
>>> >>
>>> >> say matching_chars('24680', '19584203'); # says 8420
>>> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>> >>
>>> >> Perl 6:
>>> >> sub matching_chars(Str $chars_to_match, Str $_) {
>>> >> # warnings, treats as string not variable
>>> >> m/<[$chars_to_match]>/;
>>> >> }
>>> >>
>>> >> How do I get Perl 6 to interpret a variable in the contents of a
>>> character class?
>>> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
>>> think that  Rakudo would use the 

Re: Variable character class

2019-09-01 Thread yary
Thanks for the ideas. The core issue I'm probing is runtime construction of
character classes, with an eye towards opening a documentation issue, or
maybe even an issue against the character class implementation.

Simon's workaround m:g/<{$chars.comb}>+/  is interesting, interpolating a
list which turns into alternation - but it isn't a character class. Bill's
proposal with intersecting sets - via a junction, though ∩ aka (&) also
ought to work - is short and clear, but solves a different problem.

Paul's suggestion should create a character class, but my test doesn't work:

#!/usr/bin/env perl6
use v6;

sub matching_chars(Str $chars_to_match, Str $_) {
print "Looking for $chars_to_match in '$_'- ";
my regex x { $chars_to_match ** 1 };
m/<[]>/;
}
say matching_chars('24680', '19584203');
say matching_chars('24680', '24680x');

$ ./*match.p6*
Looking for 24680 in '19584203'- Nil
Looking for 24680 in '24680x'- 「x」

Paul, can you post a more complete working test?

The below works but is "ugly", building up the character class syntax as a
string and then evaluating it

sub matching_chars(Str $chars_to_match, Str $_) {
  print "Looking for $chars_to_match in '$_'- ";

m/<{ # Interpolate contents as a regex
 "<[" ~ # Open the character class
# Add the char list, with closing bracket backslashed
$chars_to_match.subst(']','\]' ,:g) ~
"]>" # Close the character class
  }>+/; # End interpolation, plus sign for one-or-more
}
say matching_chars('24680', '19584203'); # matches 8420
say matching_chars('+/][', 'Apple ][+//e'); # matches ][+//

I see this as a shortcoming in the character class implementation.
Specifically the "literal" vs "metacharacter" distinction
 goes
out the window with enumerated character classes, it behaves like a
single-quoting. I want all the different interpolation goodness
 to work with
character classes, and it seems that requires a new syntax.

-y


On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor 
wrote:

> Using a set would be good but it doesn't give you the matching string from
> the original (which is what I thought was required) otherwise Sets would be
> my first thought.
>
> On Sun, 1 Sep 2019, 17:57 William Michels,  wrote:
>
>> Hi Yary and Paul and Simon,
>>
>> I ran into the same difficulties as Yary with repeated characters, so
>> I tried the .unique method. Then after a while, I realized that
>> problems like this might best be treated as "Set" problems in Perl6.
>> Note the Set Intersection operator "(&)" below:
>>
>> sub matching_chars(Str $a, Str $b) {
>>my @c = $a.comb.unique;
>>my @d = $b.comb.unique;
>>#say @c; say @d;
>>return @c (&) @d;
>> }
>>
>> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>>
>>
>> https://docs.perl6.org/routine/Set
>> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>>
>> HTH, Bill.
>>
>>
>> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci 
>> wrote:
>> >
>> > I'm not entirely sure if this is the correct answer, but if you define
>> your own custom character class
>> > with a 'regex' object, you can use that in the grouping.
>> >
>> > sub matching_chars(Str $chars_to_match, Str $_) {
>> > my regex x { $chars_to_match ** 1 };
>> > m/<[]>/;
>> > }
>> >
>> > The above worked for me in the very small testing I did.
>> >
>> > ~Paul
>> >
>> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>> >>
>> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
>> specifying a character class via a variable.
>> >>
>> >> Perl 5:
>> >> sub matching_chars {
>> >>   (my $chars_to_match, local $_) = @_;
>> >>   /([$chars_to_match]+)/
>> >> }
>> >>
>> >> say matching_chars('24680', '19584203'); # says 8420
>> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>> >>
>> >> Perl 6:
>> >> sub matching_chars(Str $chars_to_match, Str $_) {
>> >> # warnings, treats as string not variable
>> >> m/<[$chars_to_match]>/;
>> >> }
>> >>
>> >> How do I get Perl 6 to interpret a variable in the contents of a
>> character class?
>> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
>> think that  Rakudo would use the literal contents of $chars_to_match,
>> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
>> warning about repeated c, underscore, etc.
>> >>
>> >> -y
>> >
>> >
>> >
>> > --
>> > __
>> >
>> > :(){ :|:& };:
>>
>


Re: Variable character class

2019-09-01 Thread Simon Proctor
Using a set would be good but it doesn't give you the matching string from
the original (which is what I thought was required) otherwise Sets would be
my first thought.

On Sun, 1 Sep 2019, 17:57 William Michels,  wrote:

> Hi Yary and Paul and Simon,
>
> I ran into the same difficulties as Yary with repeated characters, so
> I tried the .unique method. Then after a while, I realized that
> problems like this might best be treated as "Set" problems in Perl6.
> Note the Set Intersection operator "(&)" below:
>
> sub matching_chars(Str $a, Str $b) {
>my @c = $a.comb.unique;
>my @d = $b.comb.unique;
>#say @c; say @d;
>return @c (&) @d;
> }
>
> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>
>
> https://docs.perl6.org/routine/Set
> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>
> HTH, Bill.
>
>
> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci  wrote:
> >
> > I'm not entirely sure if this is the correct answer, but if you define
> your own custom character class
> > with a 'regex' object, you can use that in the grouping.
> >
> > sub matching_chars(Str $chars_to_match, Str $_) {
> > my regex x { $chars_to_match ** 1 };
> > m/<[]>/;
> > }
> >
> > The above worked for me in the very small testing I did.
> >
> > ~Paul
> >
> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
> >>
> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
> specifying a character class via a variable.
> >>
> >> Perl 5:
> >> sub matching_chars {
> >>   (my $chars_to_match, local $_) = @_;
> >>   /([$chars_to_match]+)/
> >> }
> >>
> >> say matching_chars('24680', '19584203'); # says 8420
> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
> >>
> >> Perl 6:
> >> sub matching_chars(Str $chars_to_match, Str $_) {
> >> # warnings, treats as string not variable
> >> m/<[$chars_to_match]>/;
> >> }
> >>
> >> How do I get Perl 6 to interpret a variable in the contents of a
> character class?
> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
> think that  Rakudo would use the literal contents of $chars_to_match,
> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
> warning about repeated c, underscore, etc.
> >>
> >> -y
> >
> >
> >
> > --
> > __
> >
> > :(){ :|:& };:
>


Re: Variable character class

2019-09-01 Thread William Michels via perl6-users
Hi Yary and Paul and Simon,

I ran into the same difficulties as Yary with repeated characters, so
I tried the .unique method. Then after a while, I realized that
problems like this might best be treated as "Set" problems in Perl6.
Note the Set Intersection operator "(&)" below:

sub matching_chars(Str $a, Str $b) {
   my @c = $a.comb.unique;
   my @d = $b.comb.unique;
   #say @c; say @d;
   return @c (&) @d;
}

say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])


https://docs.perl6.org/routine/Set
https://docs.perl6.org/language/operators#infix_(&),_infix_∩

HTH, Bill.


On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci  wrote:
>
> I'm not entirely sure if this is the correct answer, but if you define your 
> own custom character class
> with a 'regex' object, you can use that in the grouping.
>
> sub matching_chars(Str $chars_to_match, Str $_) {
> my regex x { $chars_to_match ** 1 };
> m/<[]>/;
> }
>
> The above worked for me in the very small testing I did.
>
> ~Paul
>
> On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>>
>> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying a 
>> character class via a variable.
>>
>> Perl 5:
>> sub matching_chars {
>>   (my $chars_to_match, local $_) = @_;
>>   /([$chars_to_match]+)/
>> }
>>
>> say matching_chars('24680', '19584203'); # says 8420
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>
>> Perl 6:
>> sub matching_chars(Str $chars_to_match, Str $_) {
>> # warnings, treats as string not variable
>> m/<[$chars_to_match]>/;
>> }
>>
>> How do I get Perl 6 to interpret a variable in the contents of a character 
>> class?
>> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think 
>> that  Rakudo would use the literal contents of $chars_to_match, instead it's 
>> using the literal chars "$ c h a r s _ t o _ m a t c h" and warning about 
>> repeated c, underscore, etc.
>>
>> -y
>
>
>
> --
> __
>
> :(){ :|:& };:


Re: Variable character class

2019-09-01 Thread Simon Proctor
sub contains ( Str $chars, Str $_ ) {
  m:g/<{$chars.comb}>+/
};

This will return all the sets of matching strings but it is doing runtime
evaluation of your character string so it's a bit slow.

On Sun, 1 Sep 2019 at 04:59, Paul Procacci  wrote:

> I'm not entirely sure if this is the correct answer, but if you define
> your own custom character class
> with a 'regex' object, you can use that in the grouping.
>
> sub matching_chars(Str $chars_to_match, Str $_) {
> my regex x { $chars_to_match ** 1 };
> m/<[]>/;
> }
>
> The above worked for me in the very small testing I did.
>
> ~Paul
>
> On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
>
>> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying
>> a character class via a variable.
>>
>> Perl 5:
>> sub matching_chars {
>>   (my $chars_to_match, local $_) = @_;
>>   /([$chars_to_match]+)/
>> }
>>
>> say matching_chars('24680', '19584203'); # says 8420
>> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>>
>> Perl 6:
>> sub matching_chars(Str $chars_to_match, Str $_) {
>> # warnings, treats as string not variable
>> m/<[$chars_to_match]>/;
>> }
>>
>> How do I get Perl 6 to interpret a variable in the contents of a
>> character class?
>> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
>> think that  Rakudo would use the literal contents of $chars_to_match,
>> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
>> warning about repeated c, underscore, etc.
>>
>> -y
>>
>
>
> --
> __
>
> :(){ :|:& };:
>


-- 
Simon Proctor
Cognoscite aliquid novum cotidie

http://www.khanate.co.uk/


Re: Variable character class

2019-08-31 Thread Paul Procacci
I'm not entirely sure if this is the correct answer, but if you define your
own custom character class
with a 'regex' object, you can use that in the grouping.

sub matching_chars(Str $chars_to_match, Str $_) {
my regex x { $chars_to_match ** 1 };
m/<[]>/;
}

The above worked for me in the very small testing I did.

~Paul

On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:

> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying
> a character class via a variable.
>
> Perl 5:
> sub matching_chars {
>   (my $chars_to_match, local $_) = @_;
>   /([$chars_to_match]+)/
> }
>
> say matching_chars('24680', '19584203'); # says 8420
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>
> Perl 6:
> sub matching_chars(Str $chars_to_match, Str $_) {
> # warnings, treats as string not variable
> m/<[$chars_to_match]>/;
> }
>
> How do I get Perl 6 to interpret a variable in the contents of a character
> class?
> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think
> that  Rakudo would use the literal contents of $chars_to_match, instead
> it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning
> about repeated c, underscore, etc.
>
> -y
>


-- 
__

:(){ :|:& };:


Variable character class

2019-08-31 Thread yary
I found something easy in Perl 5 that's puzzling me in Perl 6- specifying a
character class via a variable.

Perl 5:
sub matching_chars {
  (my $chars_to_match, local $_) = @_;
  /([$chars_to_match]+)/
}

say matching_chars('24680', '19584203'); # says 8420
say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//

Perl 6:
sub matching_chars(Str $chars_to_match, Str $_) {
# warnings, treats as string not variable
m/<[$chars_to_match]>/;
}

How do I get Perl 6 to interpret a variable in the contents of a character
class?
>From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think
that  Rakudo would use the literal contents of $chars_to_match, instead
it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning
about repeated c, underscore, etc.

-y