Okay, a couple of things...

  1. I don't really understand your example of "$page->[$id]->{'parent'}". I don't see how that's a hash of hashes.

  2. I underestimated the problem, because of the "|" issue. When I've seen this problem before, the only important issues were:
    1. Nest/tab the children properly
    2. Do something special for hte first/last child
    An example of "pipeless" output is in the method "render_node_nopipe" below

  3. Even with the pipes, I still think it's cleaner to use a recursive method -- BUT, instead of passing an "indent" depth, you pass an acctuall padding string, that contains the pipes. The only real new complexity is that when dealing with each node, it's not enough to know if that node is the last of it's siblings, you also have to know if it's parent was the last of it's siblings (in order to build up the padding string properly).
    An example of the recursive solution is the method "render_node" below.

  4. It should be clear how to make this generate the appropriate HTML (<img src..>) output by modifing the single letter vars.

#!/usr/local/bin/perl -w use strict; my $d = " #"; my $t = " +"; my $l = " \\"; my $p = " |"; my $s = " "; my $tree = { 'one' => { 'two' => { 'five' => { 'boo' => { 'baz' => { }, }, }, }, 'three' => { 'four' => { }, 'yak' => { }, 'six' => { 'foo' => { 'yuk' => { }, }, 'ggg' => { }, }, }, }, }; print "with pipes...\n" . &render_node($tree->{'one'}, 'one'); print "no pipes...\n" . &render_node_tab($tree->{'one'}, 'one'); sub render_node { my ($tree, $node, $tok, $pad, $last) = @_; $tok = $s unless defined $tok; $pad = "" unless defined $pad; $last = 1 unless defined $last; my $out = $pad . $tok . $d . $node . "\n"; my @kids = sort keys %{$tree}; for (my $i = 0; $i < scalar @kids; $i++) { my $knode = $kids[$i]; my $ktree = $tree->{$knode}; my $klast = ($i+1 == scalar @kids); my $ktok = ($klast ? $l : $t); my $kpad = $pad . ($last ? $s : $p); $out .= &render_node($ktree, $knode, $ktok, $kpad, $klast); } return $out; } sub render_node_tab { my ($tree, $node, $tok, $indent) = @_; $tok = $s unless defined $tok; $indent = 0 unless defined $indent; my $out = ($s x $indent) . $tok . $d . $node . "\n"; my @kids = sort keys %{$tree}; for (my $i = 0; $i < scalar @kids; $i++) { my $knode = $kids[$i]; my $ktree = $tree->{$knode}; my $ktok = ($i+1 == scalar @kids) ? $l : $t; my $kindent = $indent+1; $out .= &render_node_tab($ktree, $knode, $ktok, $kindent); } return $out; } __END__ with pipes... #one + #three | + #four | + #six | | + #foo | | | \ #yuk | | \ #ggg | \ #yak \ #two \ #five \ #boo \ #baz no pipes... #one + #three + #four + #six + #foo \ #yuk \ #ggg \ #yak \ #two \ #five \ #boo \ #baz

In reply to Re: (cLive ;-) Re: representing a tree graphically... by hossman
in thread representing a tree graphically... by cLive ;-)

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.