Peamasii has asked for the wisdom of the Perl Monks concerning the following question:

I have a somewhat novice question about creating a tree structure out of a flat set of data. I basically want to take a number of arrays and store them in one structure.

To start with, I need to know how to store an array of values in another array, in code like this:

sub recursive_store { $query[$level] = "sql code here"; $query[$level]->execute; while((_STORAGE[$level])=$query[$level]->fetchrow_array) { print _STORAGE[$level][0]; } } &recursive_store($level);
I'm not sure what syntax to use for storing and then retrieving the arrays returned by the while condition. The purpose is then to be able to go:
print _STORAGE[$anylevel][$anyindex];
Basically I need to access an array of arrays but the samples I've seen weren't really clear on how to achieve that.

Replies are listed 'Best First'.
Re: Two-dimensional dynamic array
by ccn (Vicar) on Sep 30, 2004 at 19:51 UTC
Re: Two-dimensional dynamic array
by ikegami (Patriarch) on Sep 30, 2004 at 20:17 UTC

    Your sample code required some interpretation. Feel free to correct me.

    my @_STORAGE; sub recursive_store { $query[$level] = "SELECT ..."; my $sth = $dbh->prepare($query[$level]); $sth->execute; $_STORAGE[$level]= $sth->fetchall_arrayref; $sth->finish(); } &recursive_store($level); # Example access: print _STORAGE[$level][$row][$col]; # Show what we have: use Data::Dumper; print Dumper \@_STORAGE;
      Thanks a lot for your code. I need to apologize for not using it, because I ended up with my own solution. What I do now is instead of storing the whole array, I just shift the value off and store it in a hash table. In other words I am using parallel arrays with identical subscripts, and this seems to work fine recursing over the whole structure.

      But I'll add a bookmark to your solution because it does answer the question by storing the reference to the array. Thanks!

      Just for laughs, here is my code. I am recursing through 6 levels this way, but I feel it will work for any number of levels:

      $query[$level]->execute; while((@level_data)=$query[$level]->fetchrow_array) { %treepos = (); # reset the whereclause for a new branch %pathpos = (); $treepos{$key} = shift @level_data; $pathquery[$level] = &path_sql($childlevel,$treepos{$key}); while((@path_data)=$pathquery[$level]->fetchrow_array) { $pathpos{$key} = shift @path_data; &level_proc; } } sub level_proc { $query[$childlevel] = &level_sql($childlevel,$treepos{$key}); while((@level_data)=$query[$childlevel]->fetchrow_array) { &setlevel($childlevel); $treepos{$key} = shift @level_data; &path_proc; $treepos{$key} = undef; &setlevel($parlevel); } sub path_proc { $pathquery[$level]= &path_sql($pathpos{$parkey},$treepos{$key} +); while((@path_data)=$pathquery[$level]->fetchrow_array) { $pathpos{$key} = shift @path_data; &children_proc; } } } sub children_proc { &children_sql($pathpos{$key},$childlevel); &level_proc; }

        I'm not sure what you are doing. Actually, I have no clue what you are doing, and I can't figure out without the missing functions. But I do know this code will be a nightmare to maintain. Requiring 8 functions (children_proc children_sql, level_proc, level_sql, path_proc, path_sql, setlevel and the block outside the functions) to populate a structure from a database is a sure sign something is wrong. If you want some help, please specify what you want for output (what you want your structure to look like).