如何在不使用Data :: Compare的情況下比較Perl中的兩個哈希值?如何在不使用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.
它看起來像Test :: Deep受到is_deeply的啓發。我的問題是,如何使cmp_deeply成爲測試的一部分而不是自行測試?因爲我的測試列表只有8個,但是每次我使用cmp_deeply時,它都會作爲一個測試,因此當我只有8個函數時,我的實際測試數量爲11(因爲我稱cmp_deeply爲3次)。我不想增加我的測試數量。有更可行的解決方案嗎? – biznez 2009-08-13 21:37:53
@yskhoo。每次你調用其中一個測試函數('ok','cmp_deeply'等)時,它就算是一個測試。據我所知,沒有辦法避免這種情況。如果您不想事先提交特定數量的測試,則可以在加載測試模塊時執行此操作:'使用Test :: More qw(no_plan);'。 – FMc 2009-08-13 21:52:11
您已經在http://stackoverflow.com/questions/1274756/how-can-i-use-perls-testdeepcmpdeeply-without-increasing-the-test-count中詢問了這個問題。你沒有回答爲什麼你不能增加測試的數量,或者你的數據結構如此複雜以至於你需要調用cmp_deeply三次。請回答你提出的問題,並確定真正的問題是什麼。如果您想提供更多信息,也許我們可以提供幫助。 – Ether 2009-08-14 16:46:01
見How do I test whether two arrays or hashes are equal?
Perl的常見問題和答案,你的Perl發行版的一部分。您可以通過運行查看這個答案與您perl
附帶的版本:
$ perldoc -q equal
。
什麼是凍融cmpStr和cmpStrHard之間的區別? – biznez 2009-08-13 21:05:44
比較談論哈希值時,不是詳細的足夠的短語。有很多方法可以比較哈希值:
它們是否具有相同數量的密鑰?
if (%a == %b) {
print "they have the same number of keys\n";
} else {
print "they don't have the same number of keys\n";
}
這兩個哈希鍵是否相同?
if (%a != %b) {
print "they don't have the same number of keys\n";
} else {
my %cmp = map { $_ => 1 } keys %a;
for my $key (keys %b) {
last unless exists $cmp{$key};
delete $cmp{$key};
}
if (%cmp) {
print "they don't have the same keys\n";
} else {
print "they have the same keys\n";
}
}
它們在兩個哈希中是否具有相同的鍵和相同的值?
if (%a != %b) {
print "they don't have the same number of keys\n";
} else {
my %cmp = map { $_ => 1 } keys %a;
for my $key (keys %b) {
last unless exists $cmp{$key};
last unless $a{$key} eq $b{$key};
delete $cmp{$key};
}
if (%cmp) {
print "they don't have the same keys or values\n";
} else {
print "they have the same keys or values\n";
}
}
他們是同構的嗎(我將這個留給讀者,因爲我不是特別想從頭開始實現它)?
或其他一些相等的措施?
而且,當然,這段代碼只處理簡單的哈希值。添加複雜的數據結構使其更加複雜。
快速,骯髒,我敢肯定不是效率:
use strict;
use warnings;
use Data::Dumper;
sub compare ($$) {
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 0;
Dumper(shift) eq Dumper(shift);
}
my %a = (foo => 'bar', bar => [ 0 .. 3 ]);
my %b = (foo => 'bar', bar => [ 0 .. 3 ]);
my %c = (foo => 'bar', bar => [ 0 .. 4 ]);
print Dumper compare \%a, \%b;
print Dumper compare \%a, \%c;
爲了比較:
sub HashCompare {
my ($a, $b) = @_;
my %rhash_1 = %$a;
my %rhash_2 = %$b;
my $key = undef;
my $hash_2_line = undef;
my $hash_1_line = undef;
foreach $key (keys(%rhash_2)) {
if (exists($rhash_1{$key})) {
if ($rhash_1{$key} ne $rhash_2{$key}) {
print "key $key in $file_1 = $rhash_1{$key} & $rhash_2{$key} in $file_2\n";
}
}
}
else {
print "key $key in $file_1 is not present in $file_2\n";
#next;
}
}
foreach my $comp_key (keys %rhash_1) {
if (!exists($rhash_2{$comp_key})) {
print MYFILE "key $comp_key in $file_2 is not present in $file_1\n";
}
}
return;
}
沒有重複鍵創建哈希:
sub CreateHash {
my (@key_val_file) = @_;
my $key_count = 1;
my %hash_key_val =();
my $str4 = undef;
local $/ = undef;
foreach my $each_line (@key_val_file) {
@key_val = split(/,/, $each_line);
if (exists($hash_key_val{$key_val[0]})) {
$key_count = $key_count + 1;
$str4 = $key_val[0] . " occurence-" . $key_count;
$hash_key_val{$str4} = $key_val[1];
}
else {
$hash_key_val{$key_name} = $key_val[1];
}
}
}
$key_count = 1;
close FILE;
return %hash_key_val;
}
請爲您的答案提供解釋。 – 2012-10-26 09:25:10
$ key_name從哪裏來? – nurp 2017-11-14 10:13:25
@zakovyrya看到這個答案:https://stackoverflow.com/a/2011443/2606517 按鍵排序來自內部數據結構。
Test::Deep::NoTest具有相同的功能。
- 1. Perl中兩個哈希的比較
- 2. 在Perl中比較數值的兩個哈希值按價值比較Perl
- 3. Perl - 比較兩個嵌套哈希
- 4. Perl:比較2個哈希表值
- 5. 在perl中使用ne比較兩個哈希
- 6. 如何比較perl中的兩個多級哈希
- 7. 如何比較兩個包含〜25000個哈希的哈希?
- 8. 比較兩個哈希集?
- 9. 比較兩個密碼哈希值 - nodejs
- 10. 在Perl中比較多個哈希值的密鑰值
- 11. perl,比較哈希,子集
- 12. 如何比較兩個哈希表
- 13. BCrypt:如何比較兩個哈希?
- 14. 如何比較RSpec中的兩個哈希值?
- 15. 在不使用鍵()的情況下訪問Perl HoH中的嵌套哈希?
- 16. 如何在兩個不同的哈希中匹配哈希值
- 17. 如何在不使用引用的情況下將數組放在哈希中
- 18. 如何引用哈希值的數組的哈希值,以比較值
- 19. 如何在不使用SQL Server中的IsNull的情況下比較空值
- 20. perl使用具有多個密鑰的哈希比較文件
- 21. 蟒蛇 - 問題比較兩個哈希
- 22. hw我可以比較兩個perl哈希(鍵和值)提取差異並將其應用於哈希值javascript
- 23. 打印哈希的哈希值的兩個不同的密鑰在Perl
- 24. 排序查找在Perl在另一個哈希比較陣列的哈希
- 25. 比較兩個哈希有bcrypt(PHP)
- 26. 如何比較java中的兩個哈希集?
- 27. 如何比較兩個哈希中的元素
- 28. 比較哈希
- 29. 如何在不讀取同一個文件兩次的情況下計算兩個哈希值?
- 30. 比較在兩種情況下
那麼,你看看Data :: Compare,看看他們做了什麼。你爲什麼不想使用該模塊? – 2009-08-13 20:50:04
的可能的複製[Perl的 - 比較兩個嵌套的散列(https://stackoverflow.com/questions/37135504/perl-compare-two-nested-hash) – 2017-11-24 09:40:17