【发布时间】:2018-04-27 07:04:10
【问题描述】:
如果第二个散列中的键值对相同,我想比较散列。我不想使用 smartmatch,因为它会发出警告。
将两个哈希与整数、字符串甚至数组进行比较的最佳方法是什么?
use warnings;
use diagnostics;
my $hash1={"key_str"=>"a string", "key_int"=>4};
my $hash2={"key_str"=>"b string", "key_int"=>2};
foreach my $key ( keys(%$hash1) ) {
if ($hash1->{$key} != $hash2->{$key}) {
print($key);
}
}
预期的输出是:
Argument "b string" isn't numeric in numeric ne (!=) at hash_compare.pl line 8 (#1)
(W numeric) The indicated string was fed as an argument to an operator
that expected a numeric value instead. If you're fortunate the message
will identify which operator was so unfortunate.
Argument "a string" isn't numeric in numeric ne (!=) at hash_compare.pl line 8 (#1)
【问题讨论】: