在 Perl 6 中用另一个数组的元素过滤一个数组的元素 [英] Filtering elements of an array with elements of another array in Perl 6

查看:40
本文介绍了在 Perl 6 中用另一个数组的元素过滤一个数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤以 @search 元素开头的 @array 元素:

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 /;

问题是需要 19 秒.所以,我'预编译'grepregex,和整个程序如下所示:

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> /;

现在需要 0.444 秒.

Now it takes 0.444s.

问题:是否有内置的 Perl 6 方法来做这样的事情?类似于将 junction 插入 regex...

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 )

您需要单独创建连接字符串,它将为每个匹配评估数组连接.但是基本上不用EVAL就可以给你你想要的东西.

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.

这篇关于在 Perl 6 中用另一个数组的元素过滤一个数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆