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}});