Hello, I played a bit with your input. Maybe it would be of help.

First I added one person (Jim Morris) to have three levels in your data:

my $input = { 'Sally Smith' => { parents => ['Bob Smith', 'Rhonda Smith']}, 'Betty Freeman' => { parents => ['Earl Freeman', 'Harmony Ellis']} +, 'Betty Boop' => { parents => ['Rhonda Smith', 'Louis McFern']}, 'Jim Morris' => { parents => ['Betty Boop', 'John Wayne']}, };

You wrote that you want to descent from top, so I made new structure with children relationships and listing all people:

my $people = {}; for my $m (keys %$input) { add_person($people,$m, parents => $input->{$m}{parents}); for my $p (@{ $input->{$m}{parents} }) { add_person($people,$p,children => $m); } } sub add_person { my ($people,$name,%params) = @_; if(! defined $people->{$name}) { $people->{$name} = { children => [], parents => [] }; } for my $par (keys %params) { push @{ $people->{$name}{$par} }, ref($params{$par}) eq "ARRAY" ? @{$params{$par}} : $params +{$par}; } }

The $people structure dumped:

{ "Betty Boop" => { children => ["Jim Morris"], parents => ["Rhond +a Smith", "Louis McFern"], }, "Betty Freeman" => { children => [], parents => ["Earl Freeman", "Ha +rmony Ellis"] }, "Bob Smith" => { children => ["Sally Smith"], parents => [] }, "Earl Freeman" => { children => ["Betty Freeman"], parents => [] }, "Harmony Ellis" => { children => ["Betty Freeman"], parents => [] }, "Jim Morris" => { children => [], parents => ["Betty Boop", "John + Wayne"] }, "John Wayne" => { children => ["Jim Morris"], parents => [] }, "Louis McFern" => { children => ["Betty Boop"], parents => [] }, "Rhonda Smith" => { children => ["Sally Smith", "Betty Boop"], pare +nts => [] }, "Sally Smith" => { children => [], parents => ["Bob Smith", "Rhond +a Smith"] }, }

Now you can start with people not having any parents and recurse over children.

for my $top_level (sort keys %$people) { next if @{ $people->{$top_level}{parents} }; print_level($people,$top_level,0); } sub print_level { my ($people,$name,$level) = @_; print " "x$level,$name,"\n"; for my $child (@{ $people->{$name}{children} }) { print_level($people,$child,$level+1); } }

It produces this tree using indentation:

Bob Smith Sally Smith Earl Freeman Betty Freeman Harmony Ellis Betty Freeman John Wayne Jim Morris Louis McFern Betty Boop Jim Morris Rhonda Smith Sally Smith Betty Boop Jim Morris

Is this what you expected to see? If yes, producing some kind of javascript tree should be quite easy.

-- Roman


In reply to Re: A list of parents and children. How to make a tree? by bobr
in thread 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.