Sami_R has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Error: when joining two hashes
by Laurent_R (Canon) on Feb 19, 2020 at 10:33 UTC | |
by Sami_R (Sexton) on Feb 19, 2020 at 10:48 UTC | |
by Fletch (Bishop) on Feb 19, 2020 at 15:35 UTC | |
by haukex (Archbishop) on Feb 19, 2020 at 15:43 UTC | |
by Fletch (Bishop) on Feb 19, 2020 at 15:50 UTC | |
by Sami_R (Sexton) on Feb 20, 2020 at 12:37 UTC | |
by Laurent_R (Canon) on Feb 19, 2020 at 16:31 UTC | |
Re: Error: when joining two hashes
by haukex (Archbishop) on Feb 19, 2020 at 11:31 UTC | |
by Sami_R (Sexton) on Feb 20, 2020 at 12:39 UTC | |
Re: Error: when joining two hashes
by kcott (Archbishop) on Feb 20, 2020 at 08:28 UTC | |
by Sami_R (Sexton) on Feb 20, 2020 at 12:34 UTC | |
Re: Error: when joining two hashes
by BillKSmith (Monsignor) on Feb 19, 2020 at 16:09 UTC | |
by gurung (Sexton) on Feb 20, 2020 at 01:23 UTC | |
by BillKSmith (Monsignor) on Feb 20, 2020 at 13:39 UTC |
Back to
Seekers of Perl Wisdom