c4onastick has asked for the wisdom of the Perl Monks concerning the following question:
I've been trying to write a recursive function to generate a hash of hashes with little luck. (Recursive functions seem to be my nemesis as of late...) I'm parsing some hierarchical data from a database and trying to display it in a Win32::GUI::Treeview.
I've written a recursive function to populate the TreeView given input that looks like this:
Item1 => { SubItem1 => { SubSubItem1 => 1, SubSubItem2 => 1, SubSubItem3 => 1, }, SubItem2 => { SubSubItem1 => 1, SubSubItem2 => 1, SubSubItem3 => 1, }, }, Item2 => { ...
Which works great and looks like this:
sub hashref_to_treeview { my $hashref = shift; my $treeview = shift; # Win32::GUI::Treeview object my $parent_node = shift || 0; return unless ref($hashref) eq 'HASH'; for my $node ( sort keys %$hashref ) { my $new_node = $treeview->InsertItem(-text => $node, -parent = +> $parent_node); hashref_to_treeview($$hashref{$node}, $treeview, $new_node); } }
However, I'm having trouble getting the data out of the database to look like the input up there to feed into hashref_to_treeview. I was able to replicate the behavior I would like with a nasty set of nested foreach loops, but after finishing it looked terrible to maintain and decidedly un-perl-ish. I've summarized the flow below in pseudo-perl.
my $id = 1; my $data = {}; my $table1_results = $dbh->selectall_arrayref('SELECT field1 FROM tabl +e1 WHERE id = ?', {Slice => {}}, $id); foreach my $field1 (@$table1_results) { $data->{$id}->{$table1_results->{field_of_interest}} = undef; my $table2_results = $dbh->selectall_arrayref('SELECT field2 FROM +table2 WHERE field1 = ?', {Slice => {}}, $field1->{field1}); foreach my $field2 (@$table2_results) { $data->{$id}->{$table1_results->{field_of_interest}}->{$table2 +_results->{field_of_interest}} = undef; ... } }
I set up a slightly less ugly set of joins on the tables in question to return the values like this:
parent child1 child2 child3 -------- -------- -------- -------- value1 value2 value3 value4 value1 value2a value3 (null) ...
This seems a bit easier since the SQL changes for each level of recursion which I'd have to deal with in the function. The join's output could be returned with DBI's selectall_arrayref and then I can just recurse through each array. But I've gotten stuck on how to make that data look like the structure above. My most recent attempt looked something like this:
sub recursion_test { my $array = shift; my @results; while(@$array > 1){ push @results, recursion_test($array); } return [$$array[0], undef]; }
Which doesn't work as well as I'd hoped and leads me to my question: How can I return data structures through recursion so I can build the hash above? Also, any other suggestions for a more painless way to do this would be welcomed!
Thanks in advance for your help!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Generating a Hash of Hashes Recursively to Display Database Hierarchy
by ikegami (Patriarch) on Jun 22, 2009 at 22:14 UTC | |
by c4onastick (Friar) on Jun 23, 2009 at 15:17 UTC | |
by ikegami (Patriarch) on Jun 23, 2009 at 16:23 UTC | |
by c4onastick (Friar) on Jun 23, 2009 at 16:59 UTC | |
by ikegami (Patriarch) on Jun 23, 2009 at 17:17 UTC |