#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = (); my $hashref = \%hash; %hash = ( key1 => { key1_1 => ["value1_1","value1_2"], key1_2 => "w00t!", }, key2 => { key2_1 => "wakkawak!", key2_2 => $hashref->{key1}, # update! }, ); print Dumper($hashref);
I think the problem is that the whole %hash = ( ... ) thing is a single statement, and the actual assignment would be done after all the rest of the statement is resolved, (so after $hashref->{key2}{key1_1} is resolved).
You need to split this up in two statements:
updated: $hashref->{key1_1} is always undef, ofcourse.#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = (); my $hashref = \%hash; %hash = ( key1 => { key1_1 => ["value1_1","value1_2"], key1_2 => "w00t!", }, key2 => { key2_1 => "wakkawak!", }, ); $hash{key2}{key2_2} = $hashref->{key1}; #update! print Dumper($hashref);
In reply to Re: Populating a nested hash with itself?
by Joost
in thread Populating a nested hash with itself?
by jkva
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |