首页 > 解决方案 > 理解 Perl 中的语法

问题描述

我可能有一个非常简单的问题,关于理解 Perl 的语法是如何真正起作用的。

        unless ($alleles{$child}) {
            say (join "\t","line $.","no alleles in child") if $debug;
            next LINE;

我想要实现的是,我在一个数组中没有超过 1 个孩子,所以我需要摆脱 $child 并用数组 @children 替换它,但是有些行我不知道这是否也可以工作只是将 $child 更改为数组所以当我将其更改为数组时,这真的会遍历数组的所有项目吗?

标签: perl

解决方案


我不能完全说出你想要什么,但我认为它是以下之一:

use List::Util qw( any );

# If $alleles{$_} is true for one or more element of @children
if ( any { $alleles{$_} } @children ) {
   ...
}

use List::Util qw( all );

# If $alleles{$_} is true for every element of @children
if ( all { $alleles{$_} } @children ) {
   ...
}

# If $alleles{$_} is true for more than one element of @children
if ( ( grep { $alleles{$_} } @children ) > 1 ) {
   ...
}

推荐阅读