perl - 如何在不使用 Data::Compare 的情况下比较 Perl 中的两个哈希?

标签 perl hash

如何在不使用 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/

相关文章:

perl - 你如何覆盖替换操作?

arrays - 我似乎无法正确取消引用数组

perl - 为什么 Perl 会忽略符号和变量名之间的空格?

algorithm - 查找满足一个条件的所有组

python - 用于安全的目的是什么?

perl - 我可以使用 <> 运算符跳过整个文件吗?

xml - 如何从 Perl 中的示例 XML 文档创建模式?

hash - 你能告诉我两个产生相同 MD5 或 SHA1 哈希值的实际的、非平凡的字符串吗?

hash - 双向哈希算法?

Python 字典优化