http://www.perlmonks.org?node_id=535554


in reply to unknown number of dimensions of a multidimensional hash

The following code is taken from a game engine and manipulates "keys" of the form (\w+\.)+\w+. Each word represents a hash level.

our %GameState; # The entire game context # looks up a key (passed as the first param) and adds the value # (if provided as second param). Returns a reference to the value # list. sub Entity { my ($key, $value) = @_; return undef if ! defined $key; my $hash = \%GameGlobals::GameState; my @keys = split /\./, $key; map {$_ = lc $_} @keys; while (@keys && defined $hash) { my $key = shift @keys; if (@keys) { $hash->{$key} = {} if ! exists $hash->{$key}; $hash = $hash->{$key}; } else { $hash->{$key} = [] if ! exists $hash->{$key}; push @{$hash->{$key}}, $_[1] if defined $value; return \$hash->{$key}; } } }

DWIM is Perl's answer to Gödel