显然,哈希键是以区分大小写的方式进行比较的。
$ 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";
grep
如果您使模式匹配不区分大小写,应该可以解决问题:
perl -e '%hash = ( FOO => 1 );
printf "%s\n", ( scalar(grep (/^foo$/i, keys %hash)) > 0) ? "Yes" : "No";'
如果您有多个不同拼写的键,您可能需要检查匹配是否也大于 1。