http://www.perlmonks.org?node_id=774110


in reply to Re^3: Generating a Hash of Hashes Recursively to Display Database Hierarchy
in thread Generating a Hash of Hashes Recursively to Display Database Hierarchy

Ah, my apologies! My short hand with $data got a little too short! $data really looks like this:

my $data = [ [ 'parent1', 'child1', 'grand_child1', undef, ], [ 'parent1', 'child2', 'grand_child2', 'great_grand_child1',], [ 'parent1', 'child3', undef, undef,], ];

Or:

my $data = [ [ 'parent1', 'child1' ], [ 'child1', 'grand_child1' ], [ 'parent1', 'child2' ], [ 'child2', 'grand_child2' ], [ 'grand_child2', 'great_grand_child2' ], [ 'parent1', 'child1c' ], ];

But because of our extended table structure, I'm sure how I can (easily) make it look like the second form though.

Replies are listed 'Best First'.
Re^5: Generating a Hash of Hashes Recursively to Display Database Hierarchy
by ikegami (Patriarch) on Jun 23, 2009 at 17:17 UTC

    With the top:

    while (my $row = $sth->fetch()) { for (1..$#$row) { my ($parent_id, $id) = @{$row}[$i-1, $i-0]; last if !defined($id); ... } }

    With the bottom:

    while (my $row = $sth->fetch()) { my ($parent_id, $id) = @$row; ... }