Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: threaded display of replies

by mikfire (Deacon)
on Apr 28, 2001 at 00:20 UTC ( [id://76259]=note: print w/replies, xml ) Need Help??


in reply to threaded display of replies

What if you were to turn this around a bit and not order the datastructure but added an element that preserved the order?

My idea would be to have the primary data structure look something like this and keyed by the node id.

my %created = ( AUTHOR => $author, TYPE => $node_type, TIME => $node_time, PARENT => $parent_id || '', KIDS => [], );
One pass will load the structure from the data stream. A second pass will populate the KIDS array something like this. If the datastream is in order, or you are willing to assume that any referenced parent node will be included in the stream, you could actually include this in the first pass and save a loop. Personally, I prefer to keep the data acquisition loop seperate from the data manipulation loop - it makes it easier to debug.
for my $root ( sort { $a <=> $b } keys %created ) { my $parent = $created{$root}{PARENT}; push @{$created{$parent}{KIDS}}, $root; }
One final loop, then, can print out all the nodes in, hopefully a hierchical order by using a bit of recursion ( breadth-first? )
sub print_nodes { my @kids = @_; for my $id ( @nodes ) { print "Interesting data from node\n"; if ( @{$created{$id}{KIDS} ) { print_nodes( @{$created{$id}{KIDS}; } } } # Some work to make sure we start at the parents and move # our way down correctly. grep would work just as well. my @topnodes = (); for my $id ( keys %created ) { push @topnodes, $id unless ( defined( $created{$id}{PARENT} ) ); } print_nodes( @topnodes );
mikfire

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://76259]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2025-06-21 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.