perlquestion
Sami_R
Morning Monks,
<p>I have two hash and trying to join into one hash</p>
<code>
#!/usr/bin/perl -w
use strict;
use warnings;
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 %joined_FS;
$joined_FS{$_} ||= {( %{$VAR1->{$_}||{}}, %{$VAR2->{$_}||{}} )} for keys(%$VAR1), keys(%$VAR2);
#This example was given by PerlMonks
print Dumper(\%joined_FS);
</code>
<code>
#Error:
Not a HASH reference at perl_script_work2.pl line 2441.
# Looks like your test exited with 255 just after 2.
</code>
<p>
Expected Output:
</p>
<code>
$VAR1 = {
'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
}
};
</code>
<p>
Please give me directions to fix this, many thanks.
</p>