繁体   English   中英

Perl将散列中的所有键作为已定义的值获取

[英]Perl get all keys in a hash that as defined values

if ($type eq 'running') {
    @keys = sort {${$jobs{$type}}{$a}{stime} cmp ${$jobs{$type}}{$b}{stime}} keys % {$jobs{$type}};
} elsif ($type eq 'failed' or $type eq 'interrupted') {
    @keys = sort {${$jobs{$type}}{$a}{etime} cmp ${$jobs{$type}}{$b}{etime}} keys % {$jobs{$type}};
}

 Use of uninitialized value in string comparison (cmp) at /u/eugenep/bedrock/source/br-brock/bin/../bin/brock line 585, <$BR> line 81.

我收到上述错误。 如何有效地筛选出已定义值的键?

我不想做这样的事情:

@k_w_values = ();
foreach $k ($jobs{$type}) {
    if (defined $jobs{$type}{$k}{stime}) {
       append $k to @k_w_values
    }
}       

有像单线纸吗?

grep(perlfunc)过滤密钥:

@keys = sort {${$jobs{$type}}{$a}{stime} cmp ${$jobs{$type}}{$b}{stime}}
            grep (defined($jobs{$type}->{$_}{stime}), keys % {$jobs{$type}});

@keys = sort {${$jobs{$type}}{$a}{etime} cmp ${$jobs{$type}}{$b}{etime}} 
            grep (defined($jobs{$type}->{$_}{etime}), keys % {$jobs{$type}});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM