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 =
grade => "D",
goals => "to be a fireman",
comments => "nice but dim. Must try harder."
grade => "B+",
goals => "World Peace",
comments => "Hard worker but lacking confidence"
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"
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...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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:
Outside of code tags, you may need to use entities for some characters:
- 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
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] |