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):
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.<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">
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 { 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>'; }
What's the best way to find the entire tree of replies?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...
In reply to threaded display of replies by epoptai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |