Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
why does the third line in the first script seemingly cause the key 'wrong' to magically appear in the hash, whereas the corresponding line in the second script doesn't? A brief reading of perlfunc:keys doesn't seem to give me an answer, so I guess there must be a deeper reason. Can anyone enlighten me?script 1 foreach (qw/a b c d/){$hash{$_}++}; print keys %hash, "\n"; print keys %{$hash{'wrong'}}; print keys %hash, "\n"; OUTPUT: cabd cwrongabd --------------------------------- script 2 foreach (qw/a b c d/){$hash{$_}++}; print keys %hash, "\n"; print $hash{'wrong'}; print keys %hash, "\n"; OUTPUT: cabd cabd
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: puzzling behaviour of keys function
by duff (Parson) on Dec 08, 2004 at 15:04 UTC | |
|
Re: puzzling behaviour of keys function
by ikegami (Patriarch) on Dec 08, 2004 at 15:43 UTC | |
|
Re: puzzling behaviour of keys function
by dragonchild (Archbishop) on Dec 08, 2004 at 15:00 UTC | |
|
Re: puzzling behaviour of keys function
by Anonymous Monk on Dec 08, 2004 at 15:15 UTC | |
by friedo (Prior) on Dec 08, 2004 at 15:18 UTC |