norbu09 has asked for the wisdom of the Perl Monks concerning the following question:
the problem is that the structure has no defined depth. and btw, though it looks like another forum aproach it isn't. :)root => node1 => node2 => node2.1 => node2.2 => node3
here is what i tried:
the result as you may figure out is a array with tree arrays in it without assotiation to the parent.for (my $i=0; $i < $counter; $i++) { my $result = $sth->fetchrow_hashref (); # root node if (scalar @path == 0) { $offset = $result->{'lvl'} - 1; push @path, $result->{'payload'}; push @tree, $result->{'payload'}; next; } # child node else { # going down (one step only) if (($result->{'lvl'} - $offset) > scalar @path) { my $last = $path[$#path]; push @path, $result->{'payload'}; push @{$tree[$#path][0]}, $result->{'payload'}; $a_counter = 0; } # same level elsif (($result->{'lvl'} - $offset) == scalar @path) { my $last = $path[$#path -1]; push @{$tree[$#path][$a_counter]}, $result->{'payload' +}; $a_counter++; } # going up else { my $steps = scalar @path - $result->{'lvl'} + ($offset + -1); $#path = $steps; push @path, $result->{'payload'}; my $last = $path[$#path]; push @{$tree[$#path][0]}, $result->{'payload'}; $a_counter = 0; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: dynamic hashed tree
by perrin (Chancellor) on Aug 05, 2003 at 21:14 UTC | |
by norbu09 (Initiate) on Aug 07, 2003 at 07:34 UTC |