in reply to Sorting an Array of Hashes (AoH) by its keys as a threaded message

A nudge, a push, a kick, it's all good ;D
#!/usr/bin/perl -wl use strict; ## consider this a better way to go ## let %NODE be the representation of a typical record ## and in this case, our initial thread (aka a "root" node) my %THREAD= ( node_id => 1, root_id => 0, # 0 is the node_id of 'FooStuff', a "sect +ion" parent_id => 0, # well the parent of a "root" node is a +section content => "how do I echo a value to the screen?", title => "How do I echo a value to the screen?", ,); ## at this point you've *selected* all nodes whose parent ## is %THREAD, ie, node_id => 1 ## see, no need for recursive calls to the database ## it might be worth having a 'section' attribute ## if you want to be able to search through threads based on section my @NODES = ( \%THREAD, # therad is #1 { node_id => 2, root_id => 1, parent_id => 1, content => "i dunno", title => "re: How do I echo a value to the screen?" }, { node_id => 3, root_id => 1, parent_id => 1, content => "well you use the function print", title => "re: How do I echo a value to the screen?" }, { node_id => 6, root_id => 1, parent_id => 2, content => "don't say that", title => "re: re: How do I echo a value to the screen?" }, { node_id => 9, root_id => 1, parent_id => 6, content => "you guys are dumb", title => "re: re: re: How do I echo a value to the screen?" }, ,); print_kids(\%THREAD, 1, @NODES); exit; # not the most efficient, but it will do # (you're welcome to make with the improvements as always) sub print_kids { my ($NODE, $t, @NODES) = @_; for my $ix(0..scalar @NODES) { next unless exists $NODES[$ix]->{parent_id}; # due to sideffect of splice if( $$NODE{node_id} == $NODES[$ix]->{parent_id} ) { print "$t) ", " " x (4 * $t ), "( $NODES[$ix]->{node_id} ) ", $NODES[$ix]->{title}, "\n>>>>>>>>", ">" x (4 * $t ), " $NODES[$ix]->{content}\n"; my $N = splice(@NODES,$ix,1); print_kids($N, $t+1, @NODES); } elsif( $$NODE{node_id} == $NODES[$ix]->{node_id} ) { print "0) ", "( $NODES[$ix]->{node_id} ) ", $NODES[$ix]->{title}, "\n>>>>>>>>", ">" x (4 * $t ), " $NODES[$ix]->{content}\n"; my $N = splice(@NODES,$ix,1); print_kids($N, 1, @NODES); } } return undef; } __END__ $>perl thread.txt.pl 0) ( 1 ) How do I echo a value to the screen? >>>>>>>>>>>> how do I echo a value to the screen? 1) ( 2 ) re: How do I echo a value to the screen? >>>>>>>>>>>> i dunno 2) ( 6 ) re: re: How do I echo a value to the screen? >>>>>>>>>>>>>>>> don't say that 3) ( 9 ) re: re: re: How do I echo a value to the screen? >>>>>>>>>>>>>>>>>>>> you guys are dumb 1) ( 3 ) re: How do I echo a value to the screen? >>>>>>>>>>>> well you use the function print

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

  • Comment on (crazyinsomniac) Re: Sorting an Array of Hashes (AoH) by its keys as a threaded message
  • Download Code

Replies are listed 'Best First'.
Re: (crazyinsomniac) Re: Sorting an Array of Hashes (AoH) by its keys as a threaded message
by rdfield (Priest) on Feb 20, 2002 at 09:54 UTC
    But the request specifically stated 'non-recursive' meaning this solution is not what was required.

    rdfield

      No recursive calls to a database. This one clearly doesn't recursively call to a database.

      BMaximus