Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
the problem i want to solve is the dynamic creation of a hashed tree from the result of a database query. i tried to solve it with arrays only the success was minor. what i need is a tree of the following scheme:
root => node1 => node2 => node2.1 => node2.2 => node3
the problem is that the structure has no defined depth. and btw, though it looks like another forum aproach it isn't. :)

here is what i tried:

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; } }
the result as you may figure out is a array with tree arrays in it without assotiation to the parent.
the database structure is a nested set as found at celko's "sql for smarties", just modified to have more smaller trees instead of one giant one to speed up inserts.

In reply to dynamic hashed tree by norbu09

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-18 03:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found