Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Thanks! -roduse strict; my %hash = (); $hash{a}{drinks}=1; $hash{b}{drinks}=2; print keys %hash; ## printed a b print map { $hash{$_}{drinks} } keys %hash; ## printed 1 2 my $p = \%hash; my %copy = %{ $p }; print keys %copy; ## printed a b ## - fine, %hash has been copied to %copy $copy{c}=3; print keys %hash; ## printed a b ## - ok, %copy is something else ## and %hash won't change when %copy changes. print "REF_HASH=", \%hash, ", REF_copy=", \%copy; ## REF_HASH=HASH(0xcfbf0c), REF_COPY=HASH(0xd05168) ## - yup, they're different indeed. $copy{a}{drinks}=4; print map { $hash{$_}{drinks} } keys %hash; ## printed 4 2 !?!?!?! ## I don't get it! ## Why did %hash change here but didn't before, ## when I added the new key 'c'?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash ref mind blow
by Corion (Patriarch) on Sep 24, 2008 at 16:13 UTC | |
by Anonymous Monk on Sep 24, 2008 at 16:23 UTC | |
by ikegami (Patriarch) on Sep 24, 2008 at 21:22 UTC | |
by Anonymous Monk on Sep 25, 2008 at 15:38 UTC | |
by ikegami (Patriarch) on Sep 25, 2008 at 21:40 UTC | |
|
Re: hash ref mind blow
by duckyd (Hermit) on Sep 24, 2008 at 16:27 UTC | |
by Anonymous Monk on Sep 24, 2008 at 16:51 UTC | |
by Corion (Patriarch) on Sep 24, 2008 at 16:53 UTC | |
by ikegami (Patriarch) on Sep 24, 2008 at 21:39 UTC | |
by Anonymous Monk on Sep 25, 2008 at 08:55 UTC | |
by JadeNB (Chaplain) on Dec 28, 2009 at 00:11 UTC | |
|
Re: hash ref mind blow
by NetWallah (Canon) on Sep 24, 2008 at 16:16 UTC | |
|
Re: hash ref mind blow
by merlyn (Sage) on Sep 25, 2008 at 02:56 UTC |