I have a rather unique problem (I think) that I need to solve
pretty quickly, or it could mean my job.

I have a flat hash of complex items, each with an id number,
an array of parents (parents => [number, number, number]) and
an array of children (children => [number,number,number].

I need a recursive subroutine that, when passed the flat hash,
will then build a complex one which, starting at id number 1,
will place any item which hash '1' as parent[0] into it's
children array (full item, complex), and then so on and so forth
ad nausium until the structure is complete and nothing is left
in the original flat hash.

So what was $flat->{$item}{'childeren'} (currently an array, in order
of parentage, just the childs number) (with up to hundreds of '$item's) becomes
$top->{$child1}{children}[$grandchildren] where each $grandchild
goes deeper and deeper.

I know this doesn't sound clear, but think of it as a family.
I know who is who's parent, and children, but it's a flat list.
I need to make a tree.

Can anyone help?

What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

In reply to Flat hash to structured by tame1

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.