I did not try further, but it appears to work. One thing missing is the check of the eval return value, but this was just a kind of quick proof of concept. Passing additional arguments such as an hash ref and the value to be assigned, rather than hardcoding them, seems to be an implementation details.DB<1> @keylist = qw/one two three four/; DB<2> $hash = (); DB<3> sub make_hash {$string = join '}{', @_ ; $string = '$hash{' +. $string . '} = 0'; eval $string;} DB<4> make_hash(@keylist); DB<5> x \%hash; 0 HASH(0x6004f9dc0) 'one' => HASH(0x6004f9d48) 'two' => HASH(0x6004f9c10) 'three' => HASH(0x6004f9be0) 'four' => 0 DB<6> make_hash( qw/jan feb mar apr may jun/) DB<7> x \%hash; 0 HASH(0x6004f9dc0) 'jan' => HASH(0x60064b430) 'feb' => HASH(0x60064b508) 'mar' => HASH(0x60064b538) 'apr' => HASH(0x600500860) 'may' => HASH(0x60064eab0) 'jun' => 0 'one' => HASH(0x6004f9f58) 'two' => HASH(0x600639458) 'three' => HASH(0x6006175e8) 'four' => 0
In reply to Re: define a hash-value, using hash-ref and a list of keys
by Laurent_R
in thread define a hash-value, using hash-ref and a list of keys
by janssene
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |