确定Perl中hash2中是否存在hash1数据

确定Perl中hash2中是否存在hash1数据,perl,hash,Perl,Hash,假设我有两个哈希: my %hash1 = ('file1' => 123, 'file3' => 400); my %hash2 = ('file1' => 123, 'file2' => 300, 'file3' => 400); 确定hash1中的键/值对在hash2中是否不存在的最佳方法是什么 my %hash1 = ('file1' => 123, 'file3' => 400); my %hash2 = ('file1' => 123

假设我有两个哈希:

my %hash1 = ('file1' => 123, 'file3' => 400);
my %hash2 = ('file1' => 123, 'file2' => 300, 'file3' => 400);
确定hash1中的键/值对在hash2中是否不存在的最佳方法是什么

my %hash1 = ('file1' => 123, 'file3' => 400);
my %hash2 = ('file1' => 123, 'file2' => 300, 'file3' => 400);

foreach my $key (keys %hash1){
    print "$key\t$hash1{$key}\n" if !exists $hash2{$key};
    print "$hash1{$key}\n" if $hash1{$key} != $hash2{$key};
}
它不输出任何内容,因为
%hash1
中存在的所有键也存在于
%hash2
中,并且每个键的所有值都相同


它不输出任何内容,因为
%hash1
中存在的所有键也存在于
%hash2
中,并且每个键的所有值都相同

我喜欢使用的新成对功能。(事实上,我已经使用了我自己的版本很久了,甚至在
List::Pairwise
之前)

使用严格;
使用警告;
没有“实验性”警告;
使用列表::Util qw;
我的%hash1=('file1'=>123,'file3'=>402);
我的%hash2=('file1'=>123,'file2'=>300,'file3'=>400);
我的@comp
=pairmap{$a}
pairgrep{not(存在$hash2{$a}和$hash2{$a}~~~$b)}
%哈希1
;

请注意,
$hash1{file3}
已更改为402,以创建解决方案集

我喜欢使用的新成对功能。(事实上,我已经使用了我自己的版本很久了,甚至在
List::Pairwise
之前)

使用严格;
使用警告;
没有“实验性”警告;
使用列表::Util qw;
我的%hash1=('file1'=>123,'file3'=>402);
我的%hash2=('file1'=>123,'file2'=>300,'file3'=>400);
我的@comp
=pairmap{$a}
pairgrep{not(存在$hash2{$a}和$hash2{$a}~~~$b)}
%哈希1
;

请注意,
$hash1{file3}
已更改为402,以创建解决方案集

为了澄清,所谓“键/值对”是指键和值在两个哈希中必须相同吗?因此,在您的示例中,
%hash1
中的所有键/值对都存在于
%hash2
中。为了澄清,“键/值对”是指键和值在两个哈希中必须相同吗,在您的示例中,
%hash1
中的所有键/值对都存在于
%hash2
use strict;
use warnings;

no warnings 'experimental';
use List::Util qw<pairgrep pairmap>;

my %hash1 = ('file1' => 123, 'file3' => 402);
my %hash2 = ('file1' => 123, 'file2' => 300, 'file3' => 400);

my @comp 
    = pairmap  { $a } 
      pairgrep { not ( exists $hash2{ $a } and $hash2{ $a } ~~ $b ) }
      %hash1
    ;