punkish has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: extracting data from a deep data structure
by shmem (Chancellor) on Jul 10, 2006 at 04:22 UTC | |
by punkish (Priest) on Jul 10, 2006 at 04:35 UTC |