我在使用从数组派生的哈希时遇到了一些麻烦。对于我知道存在的元素,它未能通过“存在”测试。我写了小测试代码来确认这一点。这里是:
#!/usr/local/bin/perl
my @sieve = (2, 3, 5, 7, 11, 13, 17, 19);
my %sieve_hash = @sieve;
foreach $prime (@sieve) {
if (exists($sieve_hash{$prime})) {
print "$prime exists!\n";
} else {
print "$prime DOES NOT exist.\n";
}
}
这是示例输出:
2 exists!
3 DOES NOT exist.
5 exists!
7 DOES NOT exist.
11 exists!
13 DOES NOT exist.
17 exists!
19 DOES NOT exist.
我究竟做错了什么?