如何在不使用 Data::Compare 的情况下比较 Perl 中的两个哈希?
最佳答案
最佳方法因您的目的而异。思南提到的FAQ项目是一个很好的资源:How do I test whether two arrays or hashes are equal? .在开发和调试期间(当然还有在编写单元测试时)我发现 Test::More
在比较数组、散列和复杂数据结构时很有用。一个简单的例子:
use strict;
use warnings;
my %some_data = (
a => [1, 2, 'x'],
b => { foo => 'bar', biz => 'buz' },
j => '867-5309',
);
my %other_data = (
a => [1, 2, 'x'],
b => { foo => 'bar', biz => 'buz' },
j => '867-5309x',
);
use Test::More tests => 1;
is_deeply(\%other_data, \%some_data, 'data structures should be the same');
输出:1..1
not ok 1 - data structures should be the same
# Failed test 'data structures should be the same'
# at _x.pl line 19.
# Structures begin differing at:
# $got->{j} = '867-5309x'
# $expected->{j} = '867-5309'
# Looks like you failed 1 test of 1.
关于perl - 如何在不使用 Data::Compare 的情况下比较 Perl 中的两个哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1273616/