Traverse this.

I've been hacking on this problem on-and-off for almost a week now and only succeeded in giving myself a headache. It's time to try the "infinite monkeys" approach to this problem and see if anyone gets lucky.

The code below reproduces the structure I've got. (Yes, it's self-referential. Don't lecture me on memory leaks, please.) The "rules" are noted in the comments below. I hope there's no typos.

Good luck

#!/usr/bin/perl -w use strict; my $rdb={ 'table a' => { 'aa' => { data => { key1=>'val', key3=>'val',key2=>'val', }, parent => '', children => { 'table b' => { 'ab' => "TBD", }, 'table c' => { 'ac' => "TBD", }, } }, 'ba' => { data => { key1=>'val', key3=>'val',key2=>'val', }, parent => '', children => { 'table b' => { 'bb' => "TBD", }, 'table c' => { 'bc' => "TBD", }, } }, }, 'table b' => { 'ab' => { data => { nonsense => 'foo' }, parent => 'aa', children => {}, }, 'bb' => { data => { morenonsense => 'foo' }, parent => 'bb', children => {}, }, }, 'table c' => { 'ac' => { data => { keyi => 'val' }, parent => 'aa', children => { 'table d' => { 'ae' => "TBD", 'af' => "TBD", }, 'table e' => { 'ag' => "TBD", }, }, }, 'bc' => { data => { keyi => 'val' }, parent => 'bb', children => { 'table d' => { 'be' => "TBD", 'bf' => "TBD", }, 'table e' => { 'bg' => "TBD", 'bh' => "TBD", }, }, }, }, 'table d' => { 'ae' => { data => { k => 'v', k2 => 'v', k3 => 'v' }, parent => 'ac', children => {}, }, 'af' => { data => { k => 'x', k2 => 'x', k3 => 'x' }, parent => 'ac', children => {}, }, 'be' => { data => { k => 'v', k2 => 'v', k3 => 'v' }, parent => 'bc', children => {}, }, 'bf' => { data => { k => 'x', k2 => 'x', k3 => 'x' }, parent => 'bc', children => {}, }, }, 'table e' => { 'ag' => { data => { f => 'b', b => 'k' }, parent => 'ac', children => {}, }, 'bg' => { data => { f => 'b', b => 'k' }, parent => 'bc', children => {}, }, 'bh' => { data => { f => 'b', b => 'k' }, parent => 'bc', children => {}, }, } }; # Fixes the "TBD" above $rdb->{'table c'}->{ac}->{children}->{'table d'}->{ae}= $rdb->{'table d'}->{ae}; $rdb->{'table c'}->{ac}->{children}->{'table d'}->{af}= $rdb->{'table d'}->{af}; $rdb->{'table c'}->{ac}->{children}->{'table e'}->{ag}= $rdb->{'table e'}->{ag}; $rdb->{'table a'}->{aa}->{children}->{'table b'}->{ab}= $rdb->{'table b'}->{ab}; $rdb->{'table a'}->{aa}->{children}->{'table c'}->{ac}= $rdb->{'table c'}->{ac}; $rdb->{'table c'}->{bc}->{children}->{'table d'}->{be}= $rdb->{'table d'}->{be}; $rdb->{'table c'}->{bc}->{children}->{'table d'}->{bf}= $rdb->{'table d'}->{bf}; $rdb->{'table c'}->{bc}->{children}->{'table e'}->{bg}= $rdb->{'table e'}->{bg}; $rdb->{'table c'}->{bc}->{children}->{'table e'}->{bh}= $rdb->{'table e'}->{bh}; $rdb->{'table a'}->{ba}->{children}->{'table b'}->{bb}= $rdb->{'table b'}->{bb}; $rdb->{'table a'}->{ba}->{children}->{'table c'}->{bc}= $rdb->{'table c'}->{bc}; # Given $rdb and the key "table a", traverse the structures # so that you produce the records of: # # aa ab ac ag ae # aa ab ac ag af # ba bb bc bg be # ba bb bc bg bf # ba bb bc bh be # ba bb bc bh bf # # (These can be arrays of arrays or arrays of hashes # or arrays of strings or whatever.... # but any column MUST only contain things of the same kind) # # Note that: # * each a record from each table is represented in # each "record" produced # * multiple records within a table are noted once for # each permutation # (ae and af above) # * subsequent table names are not known: you're given # only the first one. # * the keys within the hashes are guaranteed to be # unique. # * the data is not important. # * the keys (aa, ab, bb) follow no set pattern and are # strictly coincidental just in this example. # * each record will join with the proper number of # tables. # * be prepared to have "table a" be the one and only # table (boundaries!) # # NO HARD WIRING, except for the initial table name. # # [I think I keyed these tables in right.]

In reply to So you think you're good with structures? by clintp

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.