in reply to map & arrays & recursion
sub traversal { my $self = shift; my @nodes; # self is FIRST @nodes = ( $self ); for (@{ $self->{children} }) { push @nodes, $_->traversal( ); } return @nodes; }
Note how you are pushing on the result of $_->traversal( );. Your second code could equivalently be written as
sub traversal { my $self = shift; my @nodes; # self is FIRST @nodes = ( $self ); push @nodes, @{ $self->{children} }; $_->traversal() for @{ $self->{children} }; return @nodes; }
Without knowing the contents of $self->{children} or the side-effects/return values of $node->traversal(), I cannot provide any additional insight.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: map & arrays & recursion
by ikegami (Patriarch) on Oct 01, 2010 at 03:13 UTC | |
by Anonymous Monk on Oct 02, 2010 at 00:06 UTC | |
by ikegami (Patriarch) on Oct 02, 2010 at 04:21 UTC |