I'm working on a newest nodes client and need some advice with the code that generates a threaded display of root nodes and replies like this:

I've tried coding this from scratch twice and ended up with two different ways to make the same mistake!

The Newest Nodes XML Generator makes tags like this (for example a root node and reply):

<NODE nodetype="categorized question" author_user="11732" node_id="76108" createtime="20010427142501"> <NODE nodetype="categorized answer" author_user="11732" node_id="76130" createtime="20010427150308" parent_node="76108">
The strategy has been to find replies to root nodes, then find replies to the replies, and so on. But the way i'm doing it only finds replies to the depth that's been coded for. For example the following code's maximum depth is Re:(3) because there are 3 for loops, it doesn't look for Re:(4) and beyond.
sub thread { print '<ul>'; for my $node (sort {$b <=> $a} keys %created){ # for all nodes # if doesn't have a parent and isn't a user if((!$parent{$node}) && ($nodetype{$node} ne 'user')){ if($created{$node}=~/^(....)(..)(..)(..)(..)(..)$/){ $created{$node} = "$4:$5:$6 $2/$3" } print qq~$types{$nodetype{$node}} ($created{$node})<br> $content{$node} by $whom{$author{$node}}<br>~; my$d = 0; for my $p1 (sort {$a <=> $b} keys %parent){ # re if( ($parent{$p1} == $node) ){ unless($d > 0){ print '<ul>'} print qq~$content{$p1} by $whom{$author{$p1}}<br>~; $d++; my$e = 0; for my $p2 (sort {$a <=> $b} keys %parent){ # re(2) if( ($parent{$p2} == $p1) ){ unless($e > 0){ print '<ul>'} print qq~$content{$p2} by $whom{$author{$p2}}<br>~; $e++; my$f = 0; for my $p3 (sort {$a <=> $b} keys %parent){ # re(3) if( ($parent{$p3} == $p2) ){ unless($f > 0){ print '<ul>'} print qq~$content{$p3} by $whom{$author{$p3}}<br>~; $f++; } } if($f > 0){ print '</ul>'} } } if($e > 0){ print '</ul>'} } } if($d > 0){ print '</ul>'} } } print '</ul>'; }
Ugh! I tried again with a hash like the one in Re: Graphical Hierarchical Tree (that should've been a hash of arrays i guess), but had the same problem.
sub thread { my%children; for my $root (sort {$a <=> $b} keys %created){ # for all nodes my(@kids,$kids); # if doesn't have a parent and isn't a user if( ($nodetype{$root} ne 'user') ){ for my $child (sort {$a <=> $b} keys %parent){ # for all childre +n if( ($parent{$child} == $root) ){ # if this is a child push @kids, $child; } # if $kids = join '|', @kids; $children{$root} = $kids; } } } the rest is predictable noise...
What's the best way to find the entire tree of replies?

In reply to threaded display of replies by epoptai

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.