in reply to Re^2: define a hash-value, using hash-ref and a list of keys
in thread define a hash-value, using hash-ref and a list of keys

Perl does this automatically, it's called autovivification.

You only have an issue if you want to avoid it.

update

hmm thats the problem if you post from a mobile client without possibility to test...

I'll update a working solution, though using Data::Diver should help already.

update

use warnings; use strict; use Data::Dumper; my @keys="a".."d"; my %hash=(); my $href=\%hash; my $lastkey= pop @keys; for my $key (@keys) { $href = $href->{$key} //= {} ; } $href->{$lastkey} = ""; print Dumper \%hash;
--->
$ perl dive.pl $VAR1 = { 'a' => { 'b' => { 'c' => { 'd' => '' } } } };

update
fixed bug

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^4: define a hash-value, using hash-ref and a list of keys
by janssene (Initiate) on Oct 30, 2014 at 10:28 UTC

    Hi Rolf, this is what I needed... having an older perl-version,I used for my $key (@keys) { $href =  $href->{$key} = {} unless defined $href->{$key};} is this notation {} having a specific usage or name ? Anyway, many thanks for the suggestion !!!