I want to filter elements of @array
which begin with elements of @search
:
my @array = "aaaaa" .. "fffff";
my @search = "aaaa" .. "cccc";
.put for @array .grep: /^ @search /;
The problem is it takes 19 seconds. So, I 'precompile' the regex
for grep
, and the whole program looks like this:
my @array = "aaaaa" .. "fffff";
my @search = "aaaa" .. "cccc";
my $search = "/@search.join('|')/".EVAL;
.put for @array .grep: * ~~ /^ <$search> /;
Now it takes 0.444s.
The question: is there a built-in Perl 6 method to do such things? Something like inserting a junction
into a regex
...
my @array = "aaaaa" .. "fffff";
my @search = "aaaa" .. "cccc";
my $join = @search.join('|');
my $rx = rx{ ^ <{$join}> };
@array.grep( * ~~ $rx ).map( *.put )
You need to create the join string separately of it will evaluate the array join for each match. But the basically gives you what you want without using EVAL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With