Skip to content

Instantly share code, notes, and snippets.

@moritz
Created March 22, 2012 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moritz/2161803 to your computer and use it in GitHub Desktop.
Save moritz/2161803 to your computer and use it in GitHub Desktop.
Initialize hashes of arbitrary depth
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