Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
The stupid question is the question not asked
 
PerlMonks  

Re: Graphical Hierarchical Tree

by dfog (Scribe)
on Mar 04, 2001 at 19:19 UTC ( [id://62124]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Graphical Hierarchical Tree

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"; }

Replies are listed 'Best First'.
Thanks
by yojimbo (Monk) on Mar 04, 2001 at 21:14 UTC
    Thanks for that - it made sense after I stepped through it "the old-fashioned way", on paper :-)
Re: Re: Graphical Hierarchical Tree
by yojimbo (Monk) on Mar 11, 2003 at 12:17 UTC
    More than two years since I was here :-) I had a reason to use this code again, and in going through it I found a small bug. As it stands, the code produces output like this, for a particular database app I'm writing:
    86 87 `---90 | `---91 | `---92 | `---93 `---88 `---89 94
    If you change &getarticles( $NewIndent . $IndentString, $reply); to &getarticles( $IndentString . $NewIndent, $reply); it works OK:
    87 `---90 | `---91 | `---92 | `---93 `---88 `---89 94
    YMMV, I've had no time to test this thoroughly.
Re: Re: Graphical Hierarchical Tree
by Sisyphus (Chaplain) on Mar 11, 2003 at 22:58 UTC

    ++nice. However, after adding the -w switch I got lots of uninitialized value warnings. They can be avoided by including an early check in the getarticles subroutine:
    
    sub getarticles {
        my $IndentString = shift;
             my $article = shift;
        return unless $Children{$article};
        ...
    }
    
    
    
    Granted, not a big deal, but I like it better now, with strictures on.

    -- Ricardo
    use MacPerl;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://62124]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.