Perl: How to turn array into nested hash keys
I need to convert a flat list of keys into a nested hash, as follow: my $hash = {}; my @array = qw(key1 key2 lastKey Value); ToNestedHash($hash, @array); Would do this: $hash{'key1'}{'key2'}{'lastKey'} = "Value"; Axeman sub to_nested_hash { my $ref = \shift; my $h = $$ref; my $value = pop; $ref = \$$ref->{ $_ } foreach @_; $$ref = $value; return $h; } Explanation: Take the first value as a hashref Take the last value as the value to be assigned The rest are keys. Then create a SCALAR reference to the base hash. Repeatedly: Dereference the pointer to get the hash (first time) or autovivify the