我正在尝试使用Hash::Merge
合并包含一个或多个数组的两个哈希值。例如:
use strict;
use warnings;
use feature qw(say);
use Data::Dump qw(dump);
use Hash::Merge qw(merge);
my $h1 = { a => [ { aa => 1 }, 3 ] };
my $h2 = { a => [ { bb => 2 } ] };
my $hLeft = merge( $h1, $h2 );
my $hRight = merge( $h2, $h1 );
say " hLeft: " . dump($hLeft);
say " hRight: " . dump($hRight);
my $hDesired = { a => [ { aa => 1, bb => 2 }, 3 ] };
say "Desired: " . dump($hDesired);
这给出了输出:
hLeft: { a => [{ aa => 1 }, 3, { bb => 2 }] }
hRight: { a => [{ bb => 2 }, { aa => 1 }, 3] }
Desired: { a => [{ aa => 1, bb => 2 }, 3] }
如何使用Hash::Merge
获得正确的输出?
答案 0 :(得分:3)
可以使用Hash::Merge::specify_behavior
:
use warnings;
use strict;
use Data::Dump 'dump';
use Hash::Merge;
use feature 'say';
Hash::Merge::specify_behavior
( {
'SCALAR' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => sub { [ $_[0], @{$_[1]} ] },
'HASH' => sub { $_[1] },
},
'ARRAY' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => \&mergeArrays,
'HASH' => sub { $_[1] },
},
'HASH' => {
'SCALAR' => sub { $_[1] },
'ARRAY' => sub { [ values %{$_[0]}, @{$_[1]} ] },
'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'My Behavior',
);
my $h1={a=>[{aa=>1},3]};
my $h2={a=>[{bb=>2}]};
my $hMerge=Hash::Merge::merge($h1,$h2);
say "hMerge: ".dump($hMerge);
sub mergeArrays{
my ($a,$b)=@_;
my ($na,$nb)=($#$a,$#$b);
my @c;
if ($na>$nb) {
@c=@$a[($nb+1)..$na];
return mergeArrays2($a,$b,\@c,$nb);
} else {
@c=@$b[($na+1)..$nb];
return mergeArrays2($a,$b,\@c,$na);
}
}
sub mergeArrays2{
my ($a,$b,$c,$n)=@_;
my $r=[];
for my $i (0..$n) {
if (ref($a->[$i]) && ref($b->[$i])) {
push(@$r,Hash::Merge::_merge_hashes($a->[$i],$b->[$i]));
} else {
push(@$r,$a->[$i]);
}
}
push(@$r,@$c);
return $r;
}
输出:
hMerge: { a => [{ aa => 1, bb => 2 }, 3] }
答案 1 :(得分:1)
合并数组的默认行为是追加它们:
sub { [ @{$_[0]}, @{$_[1]} ] },
要获得不同的行为,必须使用Hash::Merge::specify_behavior
。
以下解决方案是LEFT_PRECEDENT,并将数组元素合并到元素:
use strict;
use warnings;
use feature qw(say);
use Data::Dump qw(dump);
use Hash::Merge qw(merge);
Hash::Merge::specify_behavior(
{ 'SCALAR' => {
'SCALAR' => sub { $_[0] },
'ARRAY' => sub { $_[0] },
'HASH' => sub { $_[0] },
},
'ARRAY' => {
'SCALAR' => sub { [ @{ $_[0] }, $_[1] ] },
'ARRAY' => sub {
my ( $left, $right ) = @_;
my @merged = @$left;
my @to_add = @$right;
for (@merged) {
last if !@to_add;
$_ = Hash::Merge::merge( $_, shift @to_add );
}
return [ @merged, @to_add ];
},
'HASH' => sub { [ @{ $_[0] }, values %{ $_[1] } ] },
},
'HASH' => {
'SCALAR' => sub { $_[0] },
'ARRAY' => sub { $_[0] },
'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) },
},
},
'My Behavior',
);
my $h1 = { a => [ { aa => 1 }, 3 ] };
my $h2 = { a => [ { bb => 2 } ] };
my $merged = merge( $h1, $h2 );
say "Merged: " . dump($merged);
输出:
Merged: { a => [{ aa => 1, bb => 2 }, 3] }