in reply to Two-dimensional dynamic array

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;

Replies are listed 'Best First'.
Re^2: Two-dimensional dynamic array
by Peamasii (Sexton) on Oct 01, 2004 at 07:54 UTC
    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).