Your code would work as expected if $VAR2 were a reference to a single hash rather than a reference to an array with one element (a hash ref).
#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; 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); OUTPUT: $VAR1 = { 'Tom' => { 'month10' => 0, 'month11' => 0, 'month15' => 0, 'month6' => 30, 'month7' => 33, 'month5' => 3, 'month13' => 0, 'month12' => 0, 'month4' => 0, 'month9' => 0, 'month3' => 15, 'month1' => 17, 'month2' => 1, 'month8' => 0, 'month14' => 0 }, 'Total' => { 'week3' => 0, 'month6' => 0, 'week1' => 0, 'month10' => 0, 'month8' => 0, 'month2' => 0, 'month4' => 0, 'month5' => 0, 'month7' => 0, 'month15' => 0, 'month11' => 0, 'month14' => 0, 'month16' => 0, 'month1' => 0, 'month3' => 0, 'month9' => 0, 'month13' => 0, 'week2' => 0 }, 'Harry' => { 'week3' => 5, 'week1' => 0, 'week2' => 5 } };
Bill

In reply to Re: Error: when joining two hashes by BillKSmith
in thread Error: when joining two hashes by Sami_R

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.