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