use strict; my $i = 0; sub Recurse { my %hash = ( 'count' => [$i++] ); my %hash2 = 10 > $i ? %{Recurse()} : (); for( keys %hash2 ) { push @{$hash{$_}}, @{$hash2{$_}}; } return \%hash; } my %test = %{Recurse()}; print join ' ', @{$test{count}};