my $data = [ [ 'parent1', 'child1a', 'child2a', undef, ], [ 'parent1', 'child1b', 'child2b', 'child3b',], [ 'parent1', 'child1c', undef, undef,], ]; #### 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)}; } #### $VAR1 = { 'parent1' => { 'child1a' => { 'child2a' => {} } } }; $VAR2 = { 'parent1' => { 'child1b' => { 'child2b' => { 'child3b' => {} } } } }; $VAR3 = { 'parent1' => { 'child1c' => {} } };