为什么我的Perl地图没有返回任何内容?

时间:2022-12-16 12:17:04

When I am running the following statement:

当我运行以下语句时:

@filtered = map {s/ //g} @outdata;

it is returning an empty list instead of the filtered list that I expected. What I am trying to do is remove every occurrence of   from an array of string (which is an XML file).

它返回一个空列表而不是我期望的过滤列表。我想要做的是删除每一次出现的 来自一个字符串数组(这是一个XML文件)。

Obviously, I am not understanding something. Can anyone tell me the correct way to do this might be, and why this isn't working for me as is?

显然,我不理解某事。任何人都可以告诉我这样做的正确方法,以及为什么这对我不起作用?

7 个解决方案

#1


10  

Try this:

@filtered = map {s/ //g; $_} @outdata;

The problem is the s operator in perl modifies $_ but actually returns the number of changes it made. So, the extra $_ at the end causes perl to return the modified string for each element of @outdata.

问题是perl中的s运算符修改了$ _但实际上返回了它所做的更改次数。因此,最后的额外$ _会导致perl返回@outdata的每个元素的修改后的字符串。

#2


15  

Note that map is going to modify your source array as well. So you could either do:

请注意,map也将修改源数组。所以你可以这样做:

map {s/ //g} @outdata;

and skip the @filtered variable altogether, or if you need to retain the originals,

并完全跳过@filtered变量,或者如果你需要保留原件,

@filtered = @outdata;
map {s/ //g} @filtered;

Although, in that case, it might be more readable to use foreach:

虽然,在这种情况下,使用foreach可能更具可读性:

s/ //g foreach @filtered;

#3


9  

Greg's answer has the problem that it will modify the original array as the $_ are passed aliased. You need:

Greg的答案有一个问题,它会修改原始数组,因为$ _被传递别名。你需要:

@filtered = map { (my $new = $_) =~ s/ //g; $new} @outdata;

#4


6  

To follow up on Tithonium's point, this will also do the trick:

为了跟进Tithonium的观点,这也将起到作用:

@filtered = map {local $_=$_; s/ //g; $_} @outdata;

The "local" ensures you're working on a copy, not the original.

“本地”确保您正在处理副本,而不是原件。

#5


5  

In perl 5.14 you could use the /r regex modifier to make non-destructive substitution.

在perl 5.14中,您可以使用/ r regex修饰符进行非破坏性替换。

@filtered = map {s/ //gr} @outdata;

#6


4  

use Algorithm::Loops "Filter";
@filtered = Filter { s/ //g } @outdata;

#7


3  

As a counterpoint to Greg's answer, you could misuse grep:

作为Greg答案的对照,你可以滥用grep:

@filtered = grep {s/ //g; 1} @outdata;

Don't do this.

不要这样做。

#1


10  

Try this:

@filtered = map {s/ //g; $_} @outdata;

The problem is the s operator in perl modifies $_ but actually returns the number of changes it made. So, the extra $_ at the end causes perl to return the modified string for each element of @outdata.

问题是perl中的s运算符修改了$ _但实际上返回了它所做的更改次数。因此,最后的额外$ _会导致perl返回@outdata的每个元素的修改后的字符串。

#2


15  

Note that map is going to modify your source array as well. So you could either do:

请注意,map也将修改源数组。所以你可以这样做:

map {s/ //g} @outdata;

and skip the @filtered variable altogether, or if you need to retain the originals,

并完全跳过@filtered变量,或者如果你需要保留原件,

@filtered = @outdata;
map {s/ //g} @filtered;

Although, in that case, it might be more readable to use foreach:

虽然,在这种情况下,使用foreach可能更具可读性:

s/ //g foreach @filtered;

#3


9  

Greg's answer has the problem that it will modify the original array as the $_ are passed aliased. You need:

Greg的答案有一个问题,它会修改原始数组,因为$ _被传递别名。你需要:

@filtered = map { (my $new = $_) =~ s/ //g; $new} @outdata;

#4


6  

To follow up on Tithonium's point, this will also do the trick:

为了跟进Tithonium的观点,这也将起到作用:

@filtered = map {local $_=$_; s/ //g; $_} @outdata;

The "local" ensures you're working on a copy, not the original.

“本地”确保您正在处理副本,而不是原件。

#5


5  

In perl 5.14 you could use the /r regex modifier to make non-destructive substitution.

在perl 5.14中,您可以使用/ r regex修饰符进行非破坏性替换。

@filtered = map {s/ //gr} @outdata;

#6


4  

use Algorithm::Loops "Filter";
@filtered = Filter { s/ //g } @outdata;

#7


3  

As a counterpoint to Greg's answer, you could misuse grep:

作为Greg答案的对照,你可以滥用grep:

@filtered = grep {s/ //g; 1} @outdata;

Don't do this.

不要这样做。