Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I love hashes!. But even more than I love hashes, I love Hashes of Hashes.

This meditation is somewhat related to A memory efficient hash, trading off speed - does it already exist?. In fact, on re-reading that node before posting this, I see aristotle briefly touched on this topic - so i guess im on old ground here.

The gist I took from that node, was pretty much 'In a HoH, unused pre-allocated space for each hashref makes for grotty memory consumtion in the overall structure'. Given a large enough HoH, significantly grotty.
There was a couple of solutions, a particularly interesting one from dragonchild here.

While recursively dumping my own large HoH out to screen, something occured to me - something Im not sure is rediculous or totally obvious.

Hashes consist of key/value pairs.
Hash keys need to be unique.
Similarly, a path through a HoH - from key to key to key to value - is equally unique. Like a Url.

So, (yup, you guessed it!) instead of filling hashref values with more hashrefs, how about simply creating a new key in the original hash, with the unique path that would have been in the HoH. So this


my $reportCard = { Johnny => { grade => "D", goals => "to be a fireman", comments => "nice but dim. Must try harder." }, Suzy => { grade => "B+", goals => "World Peace", comments => "Hard worker but lacking confidence" }, # ...etc } print $reportCard->{Johnny}->{comments};
my %reportCard = ( Johnny.grade => "D", Johnny.goals => "to be a fireman", Johnny.comments => "nice but dim. Must try harder!", Suzy.grade => "B+", Suzy.goals => "World Peace", Suzy.comments => "Hard Worker but lacking confidence" ); print $reportCard{"Johnny.comments"}

Dot syntax at no extra cost ;)
From my limited understanding, a couple of things occured to me

(i) specific Lookups, Inserts and Deletes should be as fast as any hash (?).
(ii) straight key/value pairs are easy to store, slurp and pass around
(iii) using sort( keys() ) will automatically group related data together
(iv) Less wasted memory - but keeps that HoH sensation

..and some sober practicalities...

(i) I seem to recall reading that hashes can overflow?
(ii) grouping elements by a certain criteria is pretty nasty to implement
(iii) the full path in every key must have some sort of memory penalty of its own

Is this suitable for everyday use?
In some ways it seems pretty dumb, but in others, quite elegant...especially if you were to throw in an interface and some methods to translate to and from a real HoH

Can anyone expand on the circumstances where this approach might either pay off or fail completely?

Any opinions are appreciated

time was, I could move my arms like a bird and...

In reply to flattening Complex Datastructures by Ctrl-z

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2015-11-29 18:49 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (752 votes), past polls