为什么一个空的 Perl 散列只有一个键?
“如何找出 Perl 中哈希值的大小?”的标准谷歌答案是“取keys(%hash)
的大小”:
my %h = {};
print scalar (keys (%h));
这将打印“1”。我原以为是零。另一方面。同样,
my %h = {};
$h{"a"} = "b";
$h{"x"} = "y";
print scalar keys (%h);
print "\nKey: $_" for (keys %h);
打印:
3
键:a
键:x
密钥:哈希(0x229e8)
这个额外的值从哪里来?
The standard googleable answer to "How do I find out the size of a hash in Perl?" is "take the size of keys(%hash)
":
my %h = {};
print scalar (keys (%h));
This prints '1'. I was expecting zero. On the other hand. Similarly,
my %h = {};
$h{"a"} = "b";
$h{"x"} = "y";
print scalar keys (%h);
print "\nKey: $_" for (keys %h);
Prints:
3
Key: a
Key: x
Key: HASH(0x229e8)
Where has this extra value come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
严格 和
更好的是,也将 诊断 添加到组合中:
strict and warnings are included with Perl for very good reasons. There is no reason not to use them.
Even better, add diagnostics into the mix too:
这也曾困扰过我。
请注意使用
()
而不是{}
。说明:值
{}
是对哈希的引用,而不是哈希本身。在 Perl 中,引用是一种标量值,对%h
的赋值对于分配单个标量值有特殊的处理。它将标量字符串化(在您的情况下为您提供字符串HASH(0x229e8)
),并将该键与值undef
相关联。使用
()
时,从列表到哈希的赋值会根据列表中的对创建键/值对,并且由于()
为空,因此哈希%h
变为空。This has bit me before too.
Note the use of
()
instead of{}
.Explanation: The value
{}
is a reference to a hash, rather than a hash itself. In Perl, a reference is a kind of scalar value, and the assignment to%h
has special processing for assigning a single scalar value. It stringifies the scalar (giving you the stringHASH(0x229e8)
in your case), and associates that key with the valueundef
.When using
()
, the assignment from list to hash creates key/value pairs from pairs in the list, and since()
is empty, the hash%h
becomes empty.{}
是对匿名哈希的引用。因此my %h = {}
相当于我的%h = ({} => undef)
。Perl 要求哈希键是字符串,因此当您使用引用作为键时,Perl 使用引用的字符串表示形式 (
HASH(0x229e8)
)。{}
is a reference to an anonymous hash. Somy %h = {}
is equivalent to my%h = ({} => undef)
.Perl requires hash keys to be strings, so when you use a reference as a key, Perl uses the reference's string representation (
HASH(0x229e8)
).%h
被分配哈希引用作为键,undef
作为值。输出:
正如 rafl 所建议的,
warnings
pragma 会捕获这。看看Greg Hewgill的答案以获取更正后的代码。
%h
gets assigned with a hash reference as the key andundef
as the value.Output:
As rafl suggested, the
warnings
pragma would have caught this.Look at Greg Hewgill's answer for the corrected code.