I have a DBM::Deep data structure that looks like so...

%db = ( year => { month => { day => { foo => 1, bar => 1, baz => 1, and so on..., } } } ); if year, month, day I want 'n' keys from $db{year}{month}{day} if year, month I want 'n' latest keys from the days from $db{year}{month} if year I want 'n' latest keys from the days from $db{year} else no date defined I want 'n' latest keys from %db

when I say 'latest' I mean, get all the keys from the latest day from the defined date parts. If they are less than 'n', go to the next latest day from that defined date part, and so on. Of course, if they (all the keys from the latest day from the defined date part) are more than 'n', then pick random 'n'.

So, if 2001-11-2, then get all the keys from 2001-11-2. If they are more than 'n', pick 'n' random.

If 2001-11, then get all the keys from 2001-11-(latest day). If they are more than 'n', pick 'n' random. If they are less than 'n', get more keys from 2001-11-(next latest day) till there are 'n'.

If 2001, then get all the keys from 2001-(latest month)-(latest day). And so on.

Now, I have got some code working. Besides it being just brute force (looping over each sub-data-structure), of course it takes longer and longer time, the fewer date parts I provide it. For example, if I don't provide any date, I have to unwind the entire data structure before I extract my keys. I am wondering if data-structure guru monks have any wizard method for tackling such a problem.

Update: Solved the problem. See code at Re^2: extracting data from a deep data structure and please comment.
--

when small people start casting long shadows, it is time to go to bed

In reply to extracting data from a deep data structure by punkish

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.