Created
March 22, 2012 18:57
-
-
Save moritz/2161803 to your computer and use it in GitHub Desktop.
Initialize hashes of arbitrary depth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my %h; | |
my @keys = <a b c d e>; | |
my $value = 'foo'; | |
fill %h, @keys, $value; | |
say %h.perl; | |
sub fill(%hash is rw, @keys, $value) { | |
my %h := %hash; | |
my @k = @keys; | |
my $last_key = @k.pop; | |
for @k -> $k { | |
%h{$k} //= {}; | |
%h := %h{$k} | |
} | |
%h{$last_key} = $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment