简体   繁体   English

如何确定Perl哈希是否包含映射到未定义值的键?

[英]How can I determine if a Perl hash contains a key mapping to an undefined value?

I need to determine if a Perl hash has a given key, but that key will be mapped to an undef value. 我需要确定Perl哈希是否具有给定密钥,但该密钥将映射到undef值。 Specifically, the motivation for this is seeing if boolean flags while using getopt() with a hash reference passed into it. 具体来说,这样做的动机是在使用带有散列引用的getopt()时传getopt()标志。 I've already searched both this site and google, and exists() and defined() don't seem to be applicable for the situation, they just see if the value for a given key is undefined, they don't check if the hash actually has the key. 我已经搜索了这个网站和谷歌,而且exists()defined()似乎不适用于这种情况,他们只是看看给定键的值是否未定义,他们不检查是否哈希实际上有关键。 If I an RTFM here, please point me to the manual that explains this. 如果我是RTFM,请指出解释此问题的手册。

exists() and defined() don't seem to be applicable for the situation, they just see if the value for a given key is undefined, they don't check if the hash actually has the key exists()和defined()似乎不适用于这种情况,他们只是看看给定键的值是否未定义,他们不检查哈希是否实际具有键

Incorrect. 不正确。 That is indeed what defined() does, but exists() does exactly what you want: 这确实是defined()作用,但exists()完全符合你的要求:

my %hash = (
    key1 => 'value',
    key2 => undef,
);

foreach my $key (qw(key1 key2 key3))
{
    print "\$hash{$key} exists: " . (exists $hash{$key} ? "yes" : "no") . "\n";
    print "\$hash{$key} is defined: " . (defined $hash{$key} ? "yes" : "no") . "\n";
}

produces: 生产:

$hash{key1} exists: yes
$hash{key1} is defined: yes
$hash{key2} exists: yes
$hash{key2} is defined: no
$hash{key3} exists: no
$hash{key3} is defined: no

The documentation for these two functions is available at the command-line at perldoc -f defined and perldoc -f exists (or read the documentation for all methods at perldoc perlfunc *). 这两个函数的文档可以在命令行中找到perldoc -f definedperldoc -f exists (或者阅读perldoc perlfunc *上所有方法的文档)。 The official web documentation is here: 官方网站文档在这里:

* Since you specifically mentioned RTFM and you may not be aware of the locations of the Perl documentation, let me also point out that you can get a full index of all the perldocs at perldoc perl or at http://perldoc.perl.org . * 由于您特别提到了RTFM并且您可能不知道Perl文档的位置,我还要指出您可以在perldoc perlhttp://perldoc.perl.org上获得所有perldoc perl的完整索引。 。

If I'm reading your question correctly, I think you are confused about exists . 如果我正确地阅读你的问题,我认为你对存在感到困惑。 From the documentation: 从文档:

exists EXPR 存在EXPR

Given an expression that specifies a hash element or array element, returns true if the specified element in the hash or array has ever been initialized, even if the corresponding value is undefined. 给定一个指定哈希元素或数组元素的表达式,如果哈希或数组中的指定元素已被初始化,则返回true,即使相应的值未定义。

For example: 例如:

use strict;
use warnings;

my %h = (
    foo => 1,
    bar => undef,
);

for my $k ( qw(foo bar baz) ){
    print $k, "\n" if exists $h{$k} and not defined $h{$k};
}

Short answer: 简短回答:

 if ( exists $hash{$key} and not defined $hash{$key} ) {
    ...
 }

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

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