bdalzell has asked for the wisdom of the Perl Monks concerning the following question:

Recently I returned to working on a complex program that uses a hash of hashes (H0H) as a database.

There is a section that creates a new child hash that then has to be added to the large HoH.

I had forgotten how to add an already created child hash to a hash of hashes.

Trying to Google things did not help. All the answers had you create the child hash while adding it to the HoH.

My large application needs to have a keyname which is also stored in the child hash as part of the information with in the child hash.

I am not trying to start a discussion about what is in the hash or the need for the keyname.

Here are two ways to add a preexisting child hash to the HoH:

Add the child hash directly:

 $HoH{"$key"} = ({%child_hash);

add by reference to new child hash

$HoH{"$key"} = \%child_hash;

Here is a working sample script which contains comments

#!/usr/bin/perl my %HoH= ( 'test1' => { 'keyname'=>'test1', 'price' => '10.00', 'desc' => 'the +1st test'}, 'test2' => { 'keyname'=>'test2', 'price' => '12.00', 'desc' => 'the +2nd test'}, 'test3' => { 'keyname'=>'test3', 'price' => '13.00', 'desc' => 'the +3rd test'}, 'test4' => { 'keyname'=>'test4', 'price' => '14.00', 'desc' => 'the +4th test'} ); my %child_hash = &new_hash; my $key = $child_hash{'keyname'}; ## 2 ways to add a newly made hash to the hash of hashes # directly # $HoH{"$key"} = ({%child_hash}); # or by reference $HoH{"$key"} = \%child_hash; my @keys = keys %file_tests; @keys = sort @keys; foreach my $key(@keys){ print "$key price is: $HoH{$key}{'price'}\n"; } sub new_hash { my %hash = ( 'keyname' => 'test5', 'price' => "20.00", 'desc' => "the 5th test"); return %hash; }

Replies are listed 'Best First'.
Re: Add child hash to hash of hashes.
by haukex (Archbishop) on May 29, 2017 at 18:10 UTC
    Here is a working sample script

    Well, not quite - for it to work, keys %file_tests should probably be keys %HoH instead. This is why it's a good idea to always Use strict and warnings!

    Anyway, I am not sure if you are asking a question, or just want to provide this information - if it's the latter, then this post is probably better suited to Meditations (don't repost it though, someone can move it for you).

    While the code you showed works, note that sub new_hash returns a list of values (the key/value pairs of %hash), this causes this list of values to be copied, which isn't necessary. Inside of sub new_hash, you could create a new anonymous hash by saying my $hash = { keyname => ... }; return $hash;, and then $hash holds a reference to this anonymous hash, which you can treat just like a regular scalar, meaning you can say things like my $child_hash = new_hash(); $HoH{ $child_hash->{keyname} } = $child_hash;, and all that gets copied over is the reference to the anonymous hash, while the data inside the anonymous hash doesn't get copied. (An alternative is sub new_hash { my %hash = ( ... ); return \%hash }.)

    As for your two suggestions, note that $HoH{$key} = {%child_hash}; causes the key/value pairs in the hash to be copied one more time, which could be considered a bit wasteful. So if you have a hash %child_hash, personally I'd recommend the $HoH{$key} = \%child_hash; version, since then what you are storing is a reference to %child_hash, which doesn't require the contents of the hash to be copied. The only thing to keep in mind is that because we're talking about references, any changes to %child_hash afterwards will be reflected in $HoH{$key} and vice versa!

    (Also, whenever I'm talking about copies above, note these are shallow copies, which means that if any of the values in the hash we are talking about are references, the copies of the hash will refer to the same data sub-structures.)

    See also perlreftut, perlref and perldsc.

    Also, in regards to your post here, see How do I change/delete my post?

Re: Add child hash to hash of hashes.
by bdalzell (Sexton) on May 29, 2017 at 17:28 UTC

    Sorry there is an error at the line example in the discussion:

    Add the child hash directly: which should read:

     $HoH{"$key"} = ({%child_hash});

    It is correct in the sample script.