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 <pproca...@gmail.com> 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 <not....@gmail.com> 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
>
>
>
> --
> __________________
>
> :(){ :|:& };:

Reply via email to