I have a MySQL database with data in it that I'll call rules. It's basically a map for a perl program that finds a rule based on the results of the previous rule. For example, rule 1 is run and based on the output (return) and the stages, the table is used to determine the next rule to run and stages to "be in." Here is a very simple example of the table:
* return * rule * stage1 * stage2 * next_rule * next_stage1 * next_stage2 *
***************************************************************************
*   ret1 *    1 *     A  *     00 *         2 *           B *          10 *
*   ret2 *    1 *     A  *     00 *         5 *           D *          23 *
*   bob1 *    2 *     B  *     10 *         5 *           D *          23 *
*   bob2 *    2 *     B  *     10 *         6 *           E *          44 *
*   dog1 *    3 *     B  *     10 *         6 *           E *          44 *
*   dog2 *    4 *     C  *     10 *         6 *           E *          44 *
*   cat1 *    5 *     D  *     23 *         7 *           F *          55 *
*   cat2 *    5 *     D  *     23 *         7 *           F *          56 *
*   cat3 *    5 *     D  *     23 *         7 *           F *          57 *
*   hit1 *    6 *     E  *     44 *         7 *           G *          55 *
*   hit1 *    6 *     K  *     14 *         7 *           H *          55 *
*   hit2 *    6 *     E  *     44 *         7 *           G *          56 *
*   etc.
***************************************************************************
What I'd like to do is create a representation of the tree and the different possible paths. I was thinking I could use a hash and each set of possible returns would be another level in the hash. Let's say I start off running rule 1 in stage1 A and stage2 00. This is the hash I'm trying to get to:
$hash{ret1}             = { rule => '1', stage1 => 'A', stage2 => '00', next_rule => '2', next_stage1 => 'B', next_stage2 => '10' };
$hash{ret1}{bob1}       = { rule => '2', stage1 => 'B', stage2 => '10', next_rule => '5', next_stage1 => 'D', next_stage2 => '23' };
$hash{ret1}{bob1}{cat1} = { rule => '5', stage1 => 'D', stage2 => '23', next_rule => '7', next_stage1 => 'F', next_stage2 => '55' };
$hash{ret1}{bob1}{cat2} = { rule => '5', stage1 => 'D', stage2 => '23', next_rule => '7', next_stage1 => 'F', next_stage2 => '56' };
$hash{ret1}{bob1}{cat3} = { rule => '5', stage1 => 'D', stage2 => '23', next_rule => '7', next_stage1 => 'F', next_stage2 => '57' };
$hash{ret1}{bob2}       = { rule => '2', stage1 => 'B', stage2 => '10', next_rule => '6', next_stage1 => 'E', next_stage2 => '44' };
$hash{ret1}{bob2}{hit1} = { rule => '6', stage1 => 'E', stage2 => '44', next_rule => '7', next_stage1 => 'G', next_stage2 => '55' };
$hash{ret1}{bob2}{hit2} = { rule => '6', stage1 => 'E', stage2 => '44', next_rule => '7', next_stage1 => 'G', next_stage2 => '56' };
$hash{ret2}             = { rule => '1', stage1 => 'A', stage2 => '00', next_rule => '5', next_stage1 => 'D', next_stage2 => '23' };
$hash{ret2}{cat1}       = { rule => '5', stage1 => 'D', stage2 => '23', next_rule => '7', next_stage1 => 'F', next_stage2 => '55' };
$hash{ret2}{cat2}       = { rule => '5', stage1 => 'D', stage2 => '23', next_rule => '7', next_stage1 => 'F', next_stage2 => '56' };
$hash{ret2}{cat3}       = { rule => '5', stage1 => 'D', stage2 => '23', next_rule => '7', next_stage1 => 'F', next_stage2 => '57' };
These trees will get up to 20 elements deep. I was thinking I could just keep going through the last set of keys, grab the rows from the table and append the next set of keys. The problem is I can't figure out how to use that last set. If I've traversed down to $hash{ret1}{bob1}, I'll select from my table all rows where rule => '5', stage1 => 'D', and stage2 => '23' and get cat1, cat2, and cat3. How do I say "ok, now create $hash{ret1}{bob1}{cat1}, $hash{ret1}{bob1}{cat2}, and $hash{ret1}{bob1}{cat3}?

Hopefully what I'm trying to do makes sense. If you have a better way to create a structure of this data that I can then use to print or diagram with, that's great too. This is just the way I thought the result would make for an easily traversed "tree" when done.

In reply to Getting hash key tree by jhisey

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.