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?


In reply to Re: Add child hash to hash of hashes. by haukex
in thread Add child hash to hash of hashes. by bdalzell

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.