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 children if( ($parent{$child} == $root) ){ # if this is a child push @kids, $child; } # if $kids = join '|', @kids; $children{$root} = $kids; } } } the rest is predictable noise...