从两个不同的列表中获取差值´;perl中的哈希

从两个不同的列表中获取差值´;perl中的哈希,perl,hash,Perl,Hash,有两个哈希列表: my @family1= ( { husband => "barney", wife => "betty", son => "bamm bamm", }, husband => "george", wife => "jane", son => "elroy", }, ); my @family2{ wife => "jane", }, ); 这两个列

有两个哈希列表:

my  @family1= (
{
   husband  => "barney",
   wife     => "betty",
   son      => "bamm bamm",
},
   husband => "george",
   wife    => "jane",
   son     => "elroy",
},
);

my @family2{
   wife    => "jane",
},
);
这两个列表中的键的结构都不同,我需要得到@family1中没有的键'layer',例如在本例中是“betty”

我曾想过做一些类似的事情:

foreach my $f1(@family1)
{
   foreach my $f2 (@family2)
   {
     if (($f1->{wife} ne $f2 -> {wife})
      { 
       print MYFILE Dumper ($f1->{wife});
      }
   }
}
当我做这样的事情时,并没有得到我所期望的。我得到n次f1->{layer},我只想得到:

@sameWife = ("betty");
有谁有更好的解决方案吗?谢谢

my @sameWife = grep {
      my $wife = $_;                         # wife from @family1
      grep { $wife ne $_->{wife} } @family2; # take $wife if she isn't in @family2
    }
    map { $_->{wife} }                       # we want just wife, not whole hash
    @family1;
或者更简单一点:

my @sameWife = map {
      my $wife = $_->{wife};        # wife from @family1
      my $not_in_family2 = grep { $wife ne $_->{wife} } @family2;
      $not_in_family2 ? $wife : (); # take $wife if she isn't in @family2
    }
    @family1;
不同的方法:

my %foo; # temp hash
# only use the wife from each has; parens are to seperate the two maps
$foo{$_}++ for ( map { $_->{wife} } @family1 ), map { $_->{wife} } @family2;
# only use the names that appear once
print grep { $foo{$_} == 1 } keys %foo;

谢谢现在我想再添加一个条件
my$not_in_family2=grep{$fixer ne${fixer}和$soneq“elroy”}@family2工作正常,但“ne”显示所有元素。