perl - Storing a hash in a hash -
i'm having troubles perl script. try store hash in hash. script trivial:
use data::dumper; %h1=(); $h1{name}="parent"; %h2=(); $h2{name}="child"; $h1{nested}=%h2; # store hash h2 in hash h1 print "h2:\n"; print dumper(%h2); # works print "h1{nested}:\n"; print dumper($h1{nested}); # fails the results:
h2: $var1 = 'name'; $var2 = 'child'; h1{nested}: $var1 = '1/8'; why $h1{nested} not dumped hash, kind of weird scalar (1/8)?
ps: if question sounds trivial - searched did not find asked before. pps: perl v5.10.1 (*) built x86_64-linux-gnu-thread-multi (with 53 registered patches, see perl -v more detail)
you can store hashref in hash:
$h1{nested}=\%h2; and access %h2's name doing
$h1{nested}->{name} in code, %h2 forced scalar context, shows "1/8" value, , stores that.
Comments
Post a Comment