in reply to Re: Generating a Hash of Hashes Recursively to Display Database Hierarchy
in thread Generating a Hash of Hashes Recursively to Display Database Hierarchy
Thanks ikegami! Unfortunately the database I'm dealing with doesn't quite let me simplify down to just parent-child IDs. The join I mentioned above is between about 10 tables, all with distinct sets of IDs, so the result of the join, as retrieved by selectall_arrayref looks like:
my $data = [ [ 'parent1', 'child1a', 'child2a', undef, ], [ 'parent1', 'child1b', 'child2b', 'child3b',], [ 'parent1', 'child1c', undef, undef,], ];
While playing with your solution, I got really close to what I need:
my @results; foreach my $row (@$data) { push @results, treeify(@$row); } print Dumper @results; sub treeify { my @list = @_; my $node = shift @list; return {} unless $node; return {$node => treeify(@list)}; }
Which yields:
$VAR1 = { 'parent1' => { 'child1a' => { 'child2a' => {} } } }; $VAR2 = { 'parent1' => { 'child1b' => { 'child2b' => { 'child3b' => {} } } } }; $VAR3 = { 'parent1' => { 'child1c' => {} } };
Which sort of brings me back to my original query, figuring out how to merge those three hashes.
Any thoughts?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Generating a Hash of Hashes Recursively to Display Database Hierarchy
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 |