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

发布于 2024-08-19 03:54:38 字数 226 浏览 14 评论 0原文

我需要确定 Perl 哈希是否具有给定的键,但该键将映射到 undef 值。具体来说,这样做的动机是查看在使用 getopt() 时是否有布尔标志以及传入的哈希引用。我已经搜索过这个网站和谷歌,并且 exists()define() 似乎不适用于这种情况,他们只是看看值是否对于给定的密钥未定义,他们不会检查哈希是否确实具有该密钥。如果我是 RTFM,请给我指点解释这一点的手册。

I need to determine if a Perl hash has a given key, but that key will be mapped to an undef value. Specifically, the motivation for this is seeing if boolean flags while using getopt() with a hash reference passed into it. 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. If I an RTFM here, please point me to the manual that explains this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓦然回首 2024-08-26 03:54:38

exists() 和 Defined() 似乎不适用于这种情况,它们只是查看给定键的值是否未定义,它们不检查哈希是否确实具有该键

不正确的键。这确实是 define() 所做的,但是 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";
}

产生:

$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

这两个函数的文档可以在命令行中找到 < code>perldoc -f 已定义 和 perldoc -f 存在(或阅读 perldoc perlfunc* 中所有方法的文档)。官方网络文档在这里:

* 由于您特别提到了 RTFM 并且您可能不知道 Perl 文档的位置,因此我还要指出您可以在 perldoc perl 或 at http://perldoc.perl.org

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

Incorrect. That is indeed what defined() does, but exists() does exactly what you want:

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*). 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.

蓝海 2024-08-26 03:54:38

如果我正确地阅读了您的问题,我认为您对 exists 感到困惑。从文档中:

存在 EXPR

给定一个指定a的表达式
哈希元素或数组元素,返回
true 如果指定元素在
哈希或数组曾经是
已初始化,即使相应的
值未定义。

例如:

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

If I'm reading your question correctly, I think you are confused about exists. From the documentation:

exists 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.

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};
}
羁拥 2024-08-26 03:54:38

简短回答:

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

Short answer:

 if ( exists $hash{$key} and not defined $hash{$key} ) {
    ...
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文