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 <simon.proc...@gmail.com> 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 <pproca...@gmail.com> 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/<[<x>]>/; >> } >> >> The above worked for me in the very small testing I did. >> >> ~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 >>> >> >> >> -- >> __________________ >> >> :(){ :|:& };: >> > > > -- > Simon Proctor > Cognoscite aliquid novum cotidie > > http://www.khanate.co.uk/ >