Traversing a multi-dimensional hash in Perl
问题 If you have a hash (or reference to a hash) in perl with many dimensions and you want to iterate across all values, what's the best way to do it. In other words, if we have $f->{$x}{$y}, I want something like foreach ($x, $y) (deep_keys %{$f}) { } instead of foreach $x (keys %f) { foreach $y (keys %{$f->{$x}) { } } 回答1: Here's an option. This works for arbitrarily deep hashes: sub deep_keys_foreach { my ($hashref, $code, $args) = @_; while (my ($k, $v) = each(%$hashref)) { my @newargs =