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?
In reply to Re^2: Generating a Hash of Hashes Recursively to Display Database Hierarchy
by c4onastick
in thread Generating a Hash of Hashes Recursively to Display Database Hierarchy
by c4onastick
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |