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!


In reply to Generating a Hash of Hashes Recursively to Display Database Hierarchy by c4onastick

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.