http://www.perlmonks.org?node_id=234186

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};
becomes...
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...