7

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...

5
  • What do you mean by "works for"? Does it crash after that much time? Oct 25, 2017 at 13:51
  • .put for @array .grep: @search.any; Oct 26, 2017 at 1:01
  • @BradGilbert The output will be wrong. E.g. faaaa will be filtered, but shouldn't. The filtered elements should begin with strings from @search. (I.e. I'm looking for a solution with regexes) Oct 26, 2017 at 7:12
  • 3
    @BradGilbert: you'd have to grep for *.starts-with(@search.any)
    – Christoph
    Oct 26, 2017 at 12:00
  • @Christoph Oh yes, that solves this particular case, that's my fault. :) Though I'm looking for something more general to merge strings into a regex. Oct 26, 2017 at 13:40

3 Answers 3

4
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.

2
  • Thanks! If I'm not mistaken, the function of EVAL is performed by the inner {}? Oct 31, 2017 at 20:17
  • 1
    <{ }> in a regex evaluates the inner block and puts the string result into the regex. It means you don't use the potentially dangerous EVAL method. Nov 1, 2017 at 9:44
3

You could try to speed this up by assembling the regexes.

I am not sure how to do this using pure Perl 6 but Regexp::Assemble is a Perl 5 module that can do this for Perl 5 regexes. You can use Perl 5 modules in Perl 6 code by appending a :from<Perl5> (without a preceding space) to a use statement and then accessing its exported symbols (classes, objects, routines, etc.) as if it was a Perl 6 module:

use v6;

use Regexp::Assemble:from<Perl5>;

my @array = "aaaaa" .. "fffff";
my @search = "aaaa" .. "cccc";
my $ra = Regexp::Assemble.new;
$ra.add( @search );
$ra.anchor_string_begin(1);
.put for @array.grep({so($ra.match( $_ ))});
2
  • Thanks, an interesting solution. How could I install this Perl 5 module? And yet I wonder, whether there is a pure v6 way to do it... Oct 25, 2017 at 19:09
  • 2
    @EugeneBarsky I use perlbrew. Follow the instructions at the GitHub page of Inline::Perl5 and install a relocateable perl. Then install cpanm for perlbrew, using perlbrew install-cpanm, also see documentation on metacpan.org. Then install Perl 5 module Regexp::Assemble using cpanm Regexp::Assemble.. Hope this helps! Oct 26, 2017 at 5:49
1

For this kind of search, you can easily use index

say (@array X @search).grep( { defined($^a[0].index($^a[1])) } )
    .grep( { $^a[0].index($^a[1]) == 0 } );

This creates a list of Cs, and seeks the second element of the pair within the first; it returns only the list of those that appear in the first position. defined is needed because it will return Nil if it does not find it. It's not faster than your alternative above, but it's in the same ballpark, with 0.03 system time and ~ 0.30 seconds

2
  • 1
    Wouldn't it be easier to use .starts-with() instead in this case? An I still don't understand why we cannot use only the second .grepNil == 0 will give False, so those elements will be filtered out. May 14, 2018 at 14:34
  • 1
    Nil is going to issue a warning. But starts-with makes sense.
    – jjmerelo
    May 14, 2018 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.