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!
-
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.