perl:一次来自多个阵列的GREP

发布于 2025-02-13 21:50:53 字数 581 浏览 0 评论 0原文

我有多个阵列(〜32)。我想从中删除所有空白元素。如何以短暂的方式完成(可以通过一个foreach循环或1-2个命令行)?

我尝试了以下操作,但它不起作用:

    my @refreshArrayList=("@list1", "@list2", "@list3","@list4", "@list5", "@list6" , "@list7");
    foreach my $i (@refreshArrayList) {
        $i = grep (!/^\s*$/, $i);
    }

可以说, @list1 =(“ abc”,“ def”, “” ,“ ghi”); @list2 =(“ qwe”,“” ,“ rty”,“ uy”,“ iop”),同样对于其他数组。现在,我想从所有数组中删除所有空白元素。

所需的输出应为: @List1 =(“ ABC”,“ DEF”,“ GHI”); @list2 =(“ qwe”,“ rty”,“ uy”,“ iop”)###所有空白元素都从所有数组中删除。

怎么办?

I have multiple arrays (~32). I want to remove all blank elements from them. How can it be done in a short way (may be via one foreach loop or 1-2 command lines)?

I tried the below, but it's not working:

    my @refreshArrayList=("@list1", "@list2", "@list3","@list4", "@list5", "@list6" , "@list7");
    foreach my $i (@refreshArrayList) {
        $i = grep (!/^\s*$/, $i);
    }

Let's say, @list1 = ("abc","def","","ghi"); @list2 = ("qwe","","rty","uy", "iop"), and similarly for other arrays. Now, I want to remove all blank elements from all the arrays.

Desired Output shall be: @list1 = ("abc","def","ghi"); @list2 = ("qwe","rty","uy", "iop") ### All blank elements are removed from all the arrays.

How can it be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2025-02-20 21:50:53

您可以创建列表引用的列表,然后在这些列表上创建迭代器,例如

for my $list (\@list1, \@list2, \@list3) {
     @$list = grep (!/^\s*$/, @$list);
}

当然,您也可以动态创建此列表引用的列表,即

my @list_of_lists;
push @list_of_lists, \@list1;
push @list_of_lists, \@list2;
...
for my $list (@list_of_lists) {
     @$list = grep (!/^\s*$/, @$list);
}

You can create a list of list references and then iterator over these, like

for my $list (\@list1, \@list2, \@list3) {
     @$list = grep (!/^\s*$/, @$list);
}

Of course, you could create this list of list references also dynamically, i.e.

my @list_of_lists;
push @list_of_lists, \@list1;
push @list_of_lists, \@list2;
...
for my $list (@list_of_lists) {
     @$list = grep (!/^\s*$/, @$list);
}
千仐 2025-02-20 21:50:53
@$_ = grep /\S/, @$_  for @AoA;   # @AoA = (\@ary1, \@ary2, ...)

说明

  • 首先,这使用语句修改器,“代码>用于循环语法

    (每个)修饰符的是一个迭代器:它对列表中的每个项目执行一次语句($ _依次对每个项目依靠)。



    这主要等同于循环的“正常” ,显着的区别是设置了范围,因此也无需将其拆除,增加了一小部分效率。 AE只能有一个语句;但是话又说回来,这可以是do块。没有范围意味着我们无法声明该语句的词汇变量(除非使用do block)。

    因此,语句为@$ _ = grep/\ s/, @$ _,为列表的每个元素执行。

  • 在(每个)循环的中,依次将列表迭代(“ topicalizer”)依次设置为每个元素的变量是对这些元素的别名。因此更改它会更改元素。来自 perlsyn


    如果省略了var,<代码> $ _设置为每个值。

    如果列表的任何元素是lvalue,则可以通过修改循环中的var进行修改。

    在我们的情况下grep的of,仅由至少一个非空间字符(/\ s/)组成。


我对语句模型循环进行了三向基准测试,该循环与带有和没有局部变量的“正常”环。

对于添加100E6号,我得到8-11%的加速(在桌面和服务器上),并且进行了更多涉及的计算($ r =($ r + $ _) / sqrt($ _)< / code>> )是4-5%。

侧面观察:在这两种情况下变量集。

@$_ = grep /\S/, @$_  for @AoA;   # @AoA = (\@ary1, \@ary2, ...)

Explanation

  • First, this uses the statement modifier, "inverting" the usual for loop syntax into the form STMT for LIST

    The for(each) modifier is an iterator: it executes the statement once for each item in the LIST (with $_ aliased to each item in turn).

    It is mostly equivalent to a "normal" for loop, with the notable difference being that no scope is set and so there is no need to tear it down either, adding a small measure of efficiency. Ae can have only one statement; but then again, that can be a do block. Having no scope means that we cannot declare lexical variables for the statement (unless a do block is used).

    So the statement is @$_ = grep /\S/, @$_, executed for each element of the list.

  • In a for(each) loop, the variable that is set to each element in turn as the list is iterated over ("topicalizer") is an alias to those elements. So changing it changes elements. From perlsyn

    If VAR is omitted, $_ is set to each value.

    If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop.

    In our case $_ is always an array reference, and then the underlying array is rewritten by dereferencing it (@$_) and assigning to that the output list of grep, which consists only of elements that have at least one non-space character (/\S/).


I ran a three-way benchmark, of the statement-modifier loop against a "normal" loop with and without a topical variable.

For adding 100e6 numbers I get 8-11% speedup (on both a desktop and a server) and with a more involved calculation ($r = ($r + $_ ) / sqrt($_)) it's 4-5%.

A side observation: In both cases the full for loop without a variable (using default $_ for the topicalizer) is 1-2% faster than the one with a lexical topical variable set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文