Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Error: when joining two hashes

by gurung (Sexton)
on Feb 20, 2020 at 01:23 UTC ( [id://11113217]=note: print w/replies, xml ) Need Help??


in reply to Re: Error: when joining two hashes
in thread Error: when joining two hashes

$VAR1 is HoH = Hash of Hash.
$VAR2 is AoHoH = Array of Hash of Hash.

Trying explain. Please correct me if i am wrong. It seems like Sami is trying to merge two hashes. $VAR1 is within HASH and $VAR2 is within ARRAY but no changes to its' key value. This code is to explain only. it's not perfect.
my %joined_FS; my %outer_keys; for my $var (($VAR1, $VAR2)) { if (ref $var eq "HASH") { while (my($key, $value) = each %$VAR1) { $outer_keys{$key} = $value; } } elsif (ref $var eq "ARRAY") { for my $href (@$var) { my @unq_keys = keys %$href; for my $key (@unq_keys) { if (!exists $outer_keys{$key}) { $outer_keys{$key} = $href->{$key}; } else { while (my($k, $v) = each %{$href->{$key}}) { $outer_keys{$key}->{$k} = $v; } } } } } } print Dumper \%outer_keys;

Replies are listed 'Best First'.
Re^3: Error: when joining two hashes
by BillKSmith (Monsignor) on Feb 20, 2020 at 13:39 UTC
    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
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113217]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found