perl - Assign arrays to hash subkeys -
given array of keys , array of values, can create hash these keys , values using @hash{@keys} = @vals.
however, subkeys of hash. not work: $h{"key"}{@subkeys} = @vals.
$ perl -mdata::dumper -le ' @subkeys=(qw(one two)); @vals=(1, 2); $hash{"key"}{@subkeys} = @vals; (qw(subkeys vals)) { print "$_ :\n", dumper(\@{$_}) }; print "hash: \n", dumper(\%hash);' what is:
subkeys : $var1 = [ 'one', 'two' ]; vals : $var1 = [ 1, 2 ]; hash: $var1 = { 'key' => { '2' => 2 } }; if possible, correct syntax following dumper result:
$var1 = { 'key' => { 'one' => 1, 'two' => 2 } }; it work when using temporary hash:
perl -mdata::dumper -le '@subkeys=(qw(one two)); @vals=(1, 2); @tmp{@subkeys}=@vals; $hash{"key"}={%tmp}; print dumper(\%hash)' but suspect i'm missing correct syntax without %tmp hash.
you need close hashref part in @{} slice "cast".
@{$hash{"key"}}{@subkeys} = @vals;
Comments
Post a Comment