Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re: Recursion, tree parsing.

by BUU (Prior)
on Apr 14, 2003 at 15:17 UTC ( [id://250309]=note: print w/replies, xml ) Need Help??


in reply to Re: Recursion, tree parsing.
in thread Recursion, tree parsing.

You can probably get away without checking for the existance of @{$t->{children}}.

Eh, yer probably right.

But why would I want to do:
change: if(@{$t->{children}}){ with if($#{$t->{children}} > 0){
?

Replies are listed 'Best First'.
Re: Re: Re: Recursion, tree parsing.
by demerphq (Chancellor) on Apr 15, 2003 at 07:38 UTC

    why would I want to do

    You wouldn't. :-) I _think_ the issue the OP was refering to (but actually doesnt deal with) is the possibility that children is undef. Since the dereference is a fetch it wont be automagically created and the if ( @{...} ) will choke. Personally I would replace it and the for with the following

    for my $child ( @{ $t->{children} || [] } ) { }

    Normally I dont put so much whitespace in my expressions, but I figured here it makes things clearer, When dereferencing, if $t->{children} is false then we use an empty anonymous array to iterate over. We dont have to check if there are elements, because if there are, we _will_ iterate over them, and if there are, well, we _wont_.

    BTW, Its a good idea to become quite familiar with binary trees before you play with n-ary trees. It helps to solidy the concepts. A good learning exercise is to implement a Tie::Hash using a binary tree. (Say a sorted hash implemented using a binary tree?) Incidentally any tree structure can be represented using binary trees if necessary so it pays off to understand them well.

    HTH


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found