如何在不使用Perl循环的情况下过滤数组?

时间:2022-08-18 07:27:55

Here I am trying to filter only the elements that do not have a substring world and store the results back to the same array. What is the correct way to do this in Perl?

在这里,我试图仅过滤没有子串世界的元素,并将结果存储回同一个数组。在Perl中执行此操作的正确方法是什么?

$ cat test.pl
use strict;
use warnings;

my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2');

print "@arr\n";
@arr =~ v/world/;
print "@arr\n";

$ perl test.pl
Applying pattern match (m//) to @array will act on scalar(@array) at
test.pl line 7.
Applying pattern match (m//) to @array will act on scalar(@array) at
test.pl line 7.
syntax error at test.pl line 7, near "/;"
Execution of test.pl aborted due to compilation errors.
$

I want to pass the array as an argument to a subroutine.

我想将数组作为参数传递给子例程。

I know one way would be to something like this

我知道一种方法是这样的

$ cat test.pl 
use strict;
use warnings;

my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2');
my @arrf;

print "@arr\n";

foreach(@arr) {
    unless ($_ =~ /world/i) {
       push (@arrf, $_); 
    }
}
print "@arrf\n";

$ perl test.pl
hello 1 hello 2 hello 3 world1 hello 4 world2
hello 1 hello 2 hello 3 hello 4
$

I want to know if there is a way to do it without the loop (using some simple filtering).

我想知道是否有办法在没有循环的情况下进行(使用一些简单的过滤)。

4 个解决方案

#1


33  

That would be grep():

那将是grep():

#!/usr/bin/perl

use strict;
use warnings;

my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2');
my @narr = ( );

print "@arr\n";
@narr = grep(!/world/, @arr);
print "@narr\n";

#2


11  

Use grep:

使用grep:

sub remove_worlds { grep !/world/, @_ }

For example:

例如:

@arrf = remove_worlds @arr;

Using grep is the most natural fit for your particular problem, but for completeness, you can also do it with map:

使用grep最适合您的特定问题,但为了完整性,您也可以使用map:

sub remove_worlds { map /world/ ? () : $_, @_ }

It's a bit klunky here, but map gives you a hook in case you want to process the filtered elements before discarding them.

这里有点笨拙,但是map会为你提供一个钩子,以防你想要在丢弃它们之前处理过滤后的元素。

#3


8  

Use grep

使用grep

@no_world_for_tomorrow = grep { !/world/ } @feathers;

For details, perldoc -f grep.

有关详细信息,perldoc -f grep。

#4


4  

You can use the grep function as:

您可以使用grep函数:

@arrf =  grep(!/world/, @arr);

The expression !/world/ is evaluated for each element of the array @arr and a list of elements which which the expression evaluated to be true is returned.

为数组的每个元素@arr计算表达式!/ world /,并返回表达式求值为true的元素列表。

The expression /world/ searches for the word world and is true it's present. And the expression !/world/ is true if the string world is absent.

表达/ world /搜索单词world并且它是真实存在的。如果字符串世界不存在,则表达式!/ world /为true。

#1


33  

That would be grep():

那将是grep():

#!/usr/bin/perl

use strict;
use warnings;

my @arr = ('hello 1', 'hello 2', 'hello 3', 'world1', 'hello 4', 'world2');
my @narr = ( );

print "@arr\n";
@narr = grep(!/world/, @arr);
print "@narr\n";

#2


11  

Use grep:

使用grep:

sub remove_worlds { grep !/world/, @_ }

For example:

例如:

@arrf = remove_worlds @arr;

Using grep is the most natural fit for your particular problem, but for completeness, you can also do it with map:

使用grep最适合您的特定问题,但为了完整性,您也可以使用map:

sub remove_worlds { map /world/ ? () : $_, @_ }

It's a bit klunky here, but map gives you a hook in case you want to process the filtered elements before discarding them.

这里有点笨拙,但是map会为你提供一个钩子,以防你想要在丢弃它们之前处理过滤后的元素。

#3


8  

Use grep

使用grep

@no_world_for_tomorrow = grep { !/world/ } @feathers;

For details, perldoc -f grep.

有关详细信息,perldoc -f grep。

#4


4  

You can use the grep function as:

您可以使用grep函数:

@arrf =  grep(!/world/, @arr);

The expression !/world/ is evaluated for each element of the array @arr and a list of elements which which the expression evaluated to be true is returned.

为数组的每个元素@arr计算表达式!/ world /,并返回表达式求值为true的元素列表。

The expression /world/ searches for the word world and is true it's present. And the expression !/world/ is true if the string world is absent.

表达/ world /搜索单词world并且它是真实存在的。如果字符串世界不存在,则表达式!/ world /为true。