Morning Monks,
I have two hash and trying to join into one hash
#!/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->{$_}||{}} )} fo
+r keys(%$VAR1), keys(%$VAR2);
#This example was given by PerlMonks
print Dumper(\%joined_FS);
#Error:
Not a HASH reference at perl_script_work2.pl line 2441.
# Looks like your test exited with 255 just after 2.
Expected Output:
$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
}
};
Please give me directions to fix this, many thanks.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.