Having done this sort of tree before, I know it is tougher to code than it first sounds. The following code prints out appoximately what you asked for in the command line, and should be fairly easily be tweaked to your needs. The hash is just there to emulate the database I don't have.

The main thing that is done is that all the replies at any given level are queried before getting their children. This allows us to check whether to make the pipes downward, or just leave whitespace. HTH Dave
#!perl use strict; my @RootArticles = ("ArtA", "ArtB", "ArtC"); my %Children = ( "ArtA" => "ArtD|ArtE|ArtF", "ArtE" => "ArtG|ArtH", "ArtC" => "ArtI|ArtJ", "ArtI" => "ArtK", "ArtK" => "ArtL", ); foreach (@RootArticles) { &Printout ("", $_); &getarticles("", $_); } sub getarticles { my $IndentString = shift; my $article = shift; my @Replies = (); foreach (split(/\|/, $Children{$article})) { push (@Replies, $_); } while (my $reply = pop(@Replies)) { &Printout($IndentString . '`---', $reply); my $NewIndent = (@Replies)?"| ":" "; &getarticles( $NewIndent . $IndentString, $reply); } return 1; } sub Printout { my ($IndentString, $String) = @_; print "$IndentString$String\n"; }

In reply to Re: Graphical Hierarchical Tree by dfog
in thread Graphical Hierarchical Tree by yojimbo

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.