Davious has asked for the wisdom of the Perl Monks concerning the following question:
and if that works, and the key is remembering both the string and the reference, then why does this print " Apple"?$hash{'food'} = 'Apple'; $hash{'food'}{'color'} = 'Red'; print "$hash{'food'}{'color'} $hash{'food'}\n";
I'm assuming it's because 'Apple' overwrites the reference, but wait, it gets weirder. In the following example, the values of one hash seemingly effect the values of another (completely unrelated) hash.$hash{'food'}{'color'} = 'Red'; $hash{'food'} = 'Apple';
These both print " Apple" but if you swap the first two lines like so:$hash1{'food'}{'color'} = 'Red'; $hash1{'food'} = 'Apple'; $hash2{'food'}{'color'} = 'Red'; $hash2{'food'} = 'Apple'; print "hash1: $hash1{'food'}{'color'} $hash1{'food'}\n"; print "hash2: $hash2{'food'}{'color'} $hash2{'food'}\n";
Then they both print "Red Apple"! How could %hash1 effect %hash2?$hash1{'food'} = 'Apple'; $hash1{'food'}{'color'} = 'Red'; $hash2{'food'}{'color'} = 'Red'; $hash2{'food'} = 'Apple'; print "hash1: $hash1{'food'}{'color'} $hash1{'food'}\n"; print "hash2: $hash2{'food'}{'color'} $hash2{'food'}\n";
I tested this on both Win2000 Perl 5.6.0 and FreeBSD Perl 5.005.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HoH Weirdness
by danger (Priest) on Jun 08, 2001 at 22:53 UTC | |
|
Re: HoH Weirdness
by lestrrat (Deacon) on Jun 08, 2001 at 22:21 UTC | |
by John M. Dlugosz (Monsignor) on Jun 09, 2001 at 00:19 UTC | |
by lestrrat (Deacon) on Jun 09, 2001 at 02:06 UTC | |
by chipmunk (Parson) on Jun 09, 2001 at 02:18 UTC | |
by John M. Dlugosz (Monsignor) on Jun 09, 2001 at 02:22 UTC | |
|
Re: HoH Weirdness
by dragonchild (Archbishop) on Jun 08, 2001 at 22:39 UTC | |
by merlyn (Sage) on Jun 08, 2001 at 22:48 UTC |