ido50 has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
I hope anyone can help me with the Tree::Nary module. Basically it's a module for handling a tree data structure where every node can have multiple children. Click Here for the CPAN page of the module.

I'm trying to traverse the tree (using the traverse function) and print all the nodes in the tree.
I have used the following function to do so:
sub printcats { my ($class, $tree) = @_; my $printsub = sub { my $node = shift; print $node->{data}, "\n"; }; $tree->traverse($tree, $Tree::Nary::PRE_ORDER, $Tree::Nary::TRAVERSE_ALL, -1, $printsub ); }
This function only prints the root tree. I know there's more than one node (The root) in the tree because the number-of-nodes function returns the correct number. Also, when using IN_ORDER or POST_ORDER instead of PRE_ORDER, one of the root's children is printed instead.

Why does this happen and how can I fix it to make it print the entire tree?
Thanks,
Ido.

-------------------------
Live fat, die young

Replies are listed 'Best First'.
Re: Need help with Tree::Nary
by matija (Priest) on Apr 26, 2004 at 12:17 UTC
    The man page says The traversal can be halted at any point by returning TRUE from FUNCTION.

    Your function doesn't have an explicit return so the return value is value returnd by the last statement, which is print , which returns true on success.

    Therefore, put a return 0; into the last line of your printsub subroutine.

      Thank you very much, that fixed it.

      -------------------------
      Live fat, die young