我有两个自动生成的哈希,它们具有相同数量的元素,但我不知道有多少。
我需要将第二个哈希值与第一个哈希值进行比较。
{
TAP1 => [0, 0, 0, 0, 0, 0, 144, 1300, 2963, 3031, 16699, 16711, 36, 6634, 6837, 7233, 1103],
TAP2 => [0, 0, 3291, 71, 2994, 6769, 10985],
TAP3 => [0, 2795, 16395],
}
{
TAP1 => [0, 0, 0, 0, 0, 0, 144, 130, 296, 3031, 16699, 1711, 3, 664, 6837, 723, 113],
TAP2 => [0, 0, 3291, 75, 295, 6778, 10985],
TAP3 => [0, 273, 16394],
}
如何实现这一目标?
答案 0 :(得分:1)
如果你需要知道的是否匹配,那么你可以使用Data::Compare
。如果结构匹配,它导出的Compare
函数返回 true 或如果它们不同则返回 false ,这与Perl cmp
和<=>
use strict;
use warnings 'all';
use Data::Compare 'Compare';
my %taps1 = (
TAP1 => [0, 0, 0, 0, 0, 0, 144, 1300, 2963, 3031, 16699, 16711, 36, 6634, 6837, 7233, 1103],
TAP2 => [0, 0, 3291, 71, 2994, 6769, 10985],
TAP3 => [0, 2795, 16395],
);
my %taps2 = (
TAP1 => [0, 0, 0, 0, 0, 0, 144, 130, 296, 3031, 16699, 1711, 3, 664, 6837, 723, 113],
TAP2 => [0, 0, 3291, 75, 295, 6778, 10985],
TAP3 => [0, 273, 16394],
);
printf "The hashes are %s\n",
Compare(\%taps1, \%taps2) ? 'identical' : 'different';
for my $key ( sort keys %taps1 ) {
printf "The %s elements are %s\n",
$key,
Compare($taps1{$key}, $taps2{$key}) ? 'identical' : 'different';
}
The hashes are different
The TAP1 elements are different
The TAP2 elements are different
The TAP3 elements are different