Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Need help with Tree::Nary

by ido50 (Scribe)
on Apr 26, 2004 at 10:51 UTC ( [id://348133]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://348133]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found