I am not a novice programmer, but this has been the most difficult programming task I've ever had to accomplish.

There is a big room full of about 150 people. I have a list of the names of all of the people, and their parents. Some of the people there are older, and have no living or present parents, and thus no parents on the list. The goal is to create a javascript menu of parents, and their children, until finally, the youngest have no children.

I have a list of people, and for each person, a list of their parents (if the parents are present). If the parents are not present at the gathering, they are not listed.

I've managed to get the data in a sort of hash of hash of list format like this:

$VAR1 = { 'Sally Smith' => { 'parents' => [ 'Bob Smith', 'Rhonda Smith' ] }, 'Betty Freeman' => { 'parents' => [ 'Earl Freeman', 'Harmony El +lis' ] }, 'Betty Boop' => { 'parents' => [ 'Rhonda Smith', 'Louis McFe +rn' ] }, };

How can I put this in some format (perl data structure) that I can easily turn into a menu list.

I was thinking that I would start by making a list of all of the individuals that do not have any parents. This would give me a root menu, no problem there.

Now, for each of those parents, in the root menu, I have a function called get_immediate_children($person,\%hash) which returns a reference to an array/list of children.

Man this is getting complicated fast. Is there a really easy way to do this? There must be!

What is the best format to use? I was think about something like:

$menu[$level] = 0; #<- contains several hashes of individuals and thei +r children as lists $menu[$level] = 1; #<- for each of those children, a new hash with the +ir children as lists $menu[$level] = 2; #<- for each of those children, a new hash with the +ir children as lists ... ...

, and so on, until every individual has been accounted for

Please, is there a monk available to shed some light on the problem?


In reply to A list of parents and children. How to make a tree? by tejinashi

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.