如何查找散列中的所有键在Perl中具有值

joe*_*joe 1 perl

如何确定所有哈希键是否都具有某些值?

Ala*_*avi 9

来自perldoc -f exists:

               print "Exists\n"    if exists $hash{$key};
               print "Defined\n"   if defined $hash{$key};
               print "True\n"      if $hash{$key};

               print "Exists\n"    if exists $array[$index];
               print "Defined\n"   if defined $array[$index];
               print "True\n"      if $array[$index];
Run Code Online (Sandbox Code Playgroud)

散列或数组元素只有在定义时才为真,如果存在则定义,但反过来不一定适用.


Cha*_*ens 6

订阅的结果的grep定义

my @keys_with_values = grep { defined $hash{$_} } keys %hash;
Run Code Online (Sandbox Code Playgroud)

重读你的问题,似乎你试图找出你的散列中的任何值是否未定义,在这种情况下,你可以说像

my @keys_without_values = grep { not defined $hash{$_} }, keys %hash;
if (@keys_without_values) {
    print "the following keys do not have a value: ",
        join(", ", @keys_without_values), "\n";
}
Run Code Online (Sandbox Code Playgroud)