# Make walk() augment the result list: sub walk { my ($i,$results) = @_; # If we're called with just one argument, we need to return a fresh list $results ||= []; push @$results, $i; for (@{$hash{$i}}) { walk($_,$results); # here, we don't care about the results of walk() }; return @$results; }; ... foreach my $r (@array) { my @big = walk($r); ... };