my ($subid, @children); while (($subid) = $children->fetchrow_array) { push @children, $subid; } for $subid (@children) { walktree ($subid); }