显然,哈希密钥以区分大小写的方式进行比较.
$ perl -e '%hash = ( FOO => 1 ); printf "%s\n", ( exists $hash{foo} ) ? "Yes" : "No";' No $ perl -e '%hash = ( FOO => 1 ); printf "%s\n", ( exists $hash{FOO} ) ? "Yes" : "No";' Yes
是否有更改当前脚本的设置?
您将不得不使用绑定哈希.例如Hash :: Case :: Preserve.
字符串的散列和更改大小写的相同字符串不相等.因此,除非在创建它之前在每个哈希键上调用"uc"并且在使用它之前,否则无法执行所需操作.
my %hash = (FOO => 1); my $key = 'fOo'; # or 'foo' for that matter my %lookup = map {(lc $_, $hash{$_})} keys %hash; printf "%s\n", ( exists $hash{(lc $key)} ) ? "Yes" : "No";