tejinashi has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A list of parents and children. How to make a tree?
by bobr (Monk) on Jan 01, 2010 at 21:45 UTC | |
by tejinashi (Initiate) on Jan 03, 2010 at 10:44 UTC | |
|
Re: A list of parents and children. How to make a tree?
by Anonymous Monk on Jan 01, 2010 at 21:21 UTC |