# Make walk() return a list and collect the results: sub walk { my ($i) = @_; my @local_results = $i; for (@{$hash{$i}}) { push @local_results, walk($_); }; return @local_results; }; ... foreach my $r (@array) { my @big = walk($r); ... };