如何确定 Perl 哈希是否包含映射到未定义值的键?
我需要确定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不正确的键。这确实是
define()
所做的,但是exists()
正是您想要的:产生:
这两个函数的文档可以在命令行中找到 < code>perldoc -f 已定义 和
perldoc -f 存在
(或阅读perldoc perlfunc
* 中所有方法的文档)。官方网络文档在这里:* 由于您特别提到了 RTFM 并且您可能不知道 Perl 文档的位置,因此我还要指出您可以在
perldoc perl
或 at http://perldoc.perl.org。Incorrect. That is indeed what
defined()
does, butexists()
does exactly what you want:produces:
The documentation for these two functions is available at the command-line at
perldoc -f defined
andperldoc -f exists
(or read the documentation for all methods atperldoc 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.如果我正确地阅读了您的问题,我认为您对 exists 感到困惑。从文档中:
例如:
If I'm reading your question correctly, I think you are confused about exists. From the documentation:
For example:
简短回答:
Short answer: