I showed that the original code would correctly merge two hashes. You have probably described Sami_R's problem correctly. In that case, all that is needed is another layer of indexing to correctly reference the second hash ($VAR2->[0] instead of $VAR2).
#!/usr/bin/perl -w
use strict;
use warnings;
use Test::More tests=>1;
my $VAR1 = {
'Total' => {
'month1' => 0,
'month2' => 0,
'month3' => 0,
'month4' => 0,
'month5' => 0,
'month6' => 0,
'month7' => 0,
'month8' => 0,
'month9' => 0,
'month10' => 0,
'month11' => 0,
'month13' => 0,
'month14' => 0,
'month15' => 0,
'month16' => 0
},
'Tom' => {
'month1' => 17,
'month2' => 1,
'month3' => 15,
'month4' => 0,
'month5' => 3,
'month6' => 30,
'month7' => 33,
'month8' => 0,
'month9' => 0,
'month10' => 0,
'month11' => 0,
'month12' => 0,
'month13' => 0,
'month14' => 0,
'month15' => 0
}
};
my $VAR2 = [
{
'Total' => {
'week1' => 0,
'week2' => 0,
'week3' => 0
},
'Harry' => {
'week1' => 0,
'week2' => 5,
'week3' => 5
}
}
];
my $EXPECTED = {
'Tom' => {
'month1' => 17,
'month2' => 1,
'month3' => 15,
'month4' => 0,
'month5' => 3,
'month6' => 30,
'month7' => 33,
'month8' => 0,
'month9' => 0,
'month10' => 0,
'month11' => 0,
'month12' => 0,
'month13' => 0,
'month14' => 0,
'month15' => 0
},
'Total' => {
'month1' => 0,
'month2' => 0,
'month3' => 0,
'month4' => 0,
'month5' => 0,
'month6' => 0,
'month7' => 0,
'month8' => 0,
'month9' => 0,
'month10' => 0,
'month11' => 0,
'month13' => 0,
'month14' => 0,
'month15' => 0,
'month16' => 0,
'week1' => 0,
'week2' => 0,
'week3' => 0
},
'Harry' => {
'week1' => 0,
'week2' => 5,
'week3' => 5
}
};
my %joined_FS;
$joined_FS{$_}
||= {
( %{$VAR1->{$_}||{}}, %{$VAR2->[0]->{$_}||{}} )
} for keys(%$VAR1), keys(%{$VAR2->[0]});
is_deeply(\%joined_FS, $EXPECTED, 'merge');
OUTPUT:
1..1
ok 1 - merge
|