Return 遍历数组后的布尔值
Return boolean value after iterating through an array
我正在尝试查看 keyword_objects
是否包含元素 {name=>'CRASH', status=>'+'}
。
# $bug is a reference to the below data
{
'keyword_objects' => [
bless( { 'name' => 'CRASH', 'status' => '+'}, 'SomeModule::SomeFilename' ),
bless( { 'name' => 'CUSTOMER', 'status' => '-' }, 'SomeModule::SomeFilename' ) ],
'category' => 'error'
}
我找不到类似 filter
的其他语言版本,所以我的替代方案是使用 map
.
my @isCrash = map { $_->name eq 'CRASH' && $_->status eq '+' } @{ $bug->{keyword_objects} };
这个的问题是,当没有这个关键字的时候,每次操作完,好像return一个空值。 @isCrash
成为多个空值的数组,而 if(@isCrash)
变得无用。我当然可以引入一个可以从地图操作中更改的新变量,但我觉得应该有更好的方法来做到这一点。有人请插话并分享您的知识。
您的代码将每个条目映射到逻辑表达式的标量值(true 或 false)。
尝试使用下面的代码将其映射到匹配的条目本身和不匹配的空列表。
多个空列表的总和为空列表。
my @isCrash = map { $_->name eq 'CRASH' && $_->status eq '+' ? ($_) : () } @{ $bug->{keyword_objects} };
另一种方法是使用 grep
:
# get list of "crashed" elements
my @CrashedList = grep { $_->name eq 'CRASH' && $_->status eq '+'} @{ $bug->{keyword_objects} };
# get number of "crashed" elements in scalar context
my $numberOfCrashes = grep { $_->name eq 'CRASH' && $_->status eq '+'} @{ $bug->{keyword_objects} };
在 Perl 中(实际上,在一般的 Unix 中),filter
拼写为 grep
。
my $is_crash = grep { $_->name eq 'CRASH' and $_->status eq '+' }
@{ $bug->{keyword_objects} };
grep BLOCK LIST
grep EXPR,LIST
[ ... snip ...]
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to true. In scalar context, returns the number of times the expression was true.
因此 $is_crash
将包含输入列表中表达式为真的元素数。这可以用作布尔值(因为零为假,所有其他整数为真)。
我正在尝试查看 keyword_objects
是否包含元素 {name=>'CRASH', status=>'+'}
。
# $bug is a reference to the below data
{
'keyword_objects' => [
bless( { 'name' => 'CRASH', 'status' => '+'}, 'SomeModule::SomeFilename' ),
bless( { 'name' => 'CUSTOMER', 'status' => '-' }, 'SomeModule::SomeFilename' ) ],
'category' => 'error'
}
我找不到类似 filter
的其他语言版本,所以我的替代方案是使用 map
.
my @isCrash = map { $_->name eq 'CRASH' && $_->status eq '+' } @{ $bug->{keyword_objects} };
这个的问题是,当没有这个关键字的时候,每次操作完,好像return一个空值。 @isCrash
成为多个空值的数组,而 if(@isCrash)
变得无用。我当然可以引入一个可以从地图操作中更改的新变量,但我觉得应该有更好的方法来做到这一点。有人请插话并分享您的知识。
您的代码将每个条目映射到逻辑表达式的标量值(true 或 false)。
尝试使用下面的代码将其映射到匹配的条目本身和不匹配的空列表。
多个空列表的总和为空列表。
my @isCrash = map { $_->name eq 'CRASH' && $_->status eq '+' ? ($_) : () } @{ $bug->{keyword_objects} };
另一种方法是使用 grep
:
# get list of "crashed" elements
my @CrashedList = grep { $_->name eq 'CRASH' && $_->status eq '+'} @{ $bug->{keyword_objects} };
# get number of "crashed" elements in scalar context
my $numberOfCrashes = grep { $_->name eq 'CRASH' && $_->status eq '+'} @{ $bug->{keyword_objects} };
在 Perl 中(实际上,在一般的 Unix 中),filter
拼写为 grep
。
my $is_crash = grep { $_->name eq 'CRASH' and $_->status eq '+' }
@{ $bug->{keyword_objects} };
grep BLOCK LIST
grep EXPR,LIST[ ... snip ...]
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to true. In scalar context, returns the number of times the expression was true.
因此 $is_crash
将包含输入列表中表达式为真的元素数。这可以用作布尔值(因为零为假,所有其他整数为真)。