perl:一次来自多个阵列的GREP
我有多个阵列(〜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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建列表引用的列表,然后在这些列表上创建迭代器,例如
当然,您也可以动态创建此列表引用的列表,即
You can create a list of list references and then iterator over these, like
Of course, you could create this list of list references also dynamically, i.e.
说明
首先,这使用语句修改器,“代码>用于循环语法
这主要等同于
循环的“正常”,显着的区别是设置了范围,因此也无需将其拆除,增加了一小部分效率。† AE只能有一个语句;但是话又说回来,这可以是
do
块。没有范围意味着我们无法声明该语句的词汇变量(除非使用do
block)。因此,语句为
@$ _ = grep/\ s/, @$ _
,为列表的每个元素执行。在(每个)循环的
中,依次将列表迭代(“ topicalizer”)依次设置为每个元素的变量是对这些元素的别名。因此更改它会更改元素。来自 perlsyn
在我们的情况下
grep
的of,仅由至少一个非空间字符(/\ s/
)组成。†我对语句模型循环进行了三向基准测试,该循环与带有和没有局部变量的“正常”环。
对于添加100E6号,我得到8-11%的加速(在桌面和服务器上),并且进行了更多涉及的计算(
$ r =($ r + $ _) / sqrt($ _)< / code>> )是4-5%。
侧面观察:在这两种情况下变量集。
Explanation
First, this uses the statement modifier, "inverting" the usual
for
loop syntax into the formSTMT for LIST
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 ado
block. Having no scope means that we cannot declare lexical variables for the statement (unless ado
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 perlsynIn our case
$_
is always an array reference, and then the underlying array is rewritten by dereferencing it (@$_
) and assigning to that the output list ofgrep
, 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.