foreach my $r (@array){ my @big = walk($r); ... } sub walk { my $i = shift; my @big = $i; for (@{$hash{$i}}){ push @big, walk($_); } return @big; }