l_millr has asked for the wisdom of the Perl Monks concerning the following question:
Output is:#!/usr/local/bin/perl %c={A=>'0',B=>'0',C=>'0',D=>'0'}; foreach $key (keys %c) { print "$key => $c{$key}\n"; }
Why? Shouldn't this code print all the values of %c?
=================================You know what? I answered my own question, so now it's an FYI for idjits like me.
The problem here is (1) I'm stupid and (2) I need to learn the difference between {} which creates an anonymous hash and () which specifies a list of values.
So %c={A=>'0',B=>'0',C=>'0',D=>'0'}; gives HASH() because you've told perl to add a self-contained hash as the key for the first value of %c.
Extra credit: What is the value of that first key? Nothing, because all you've specified is the key.
BUT %c=(A=>'0',B=>'0',C=>'0',D=>'0'); gives A=>0, B=>0, C=>0, D=>0 because you've told perl to add four key/value pairs to %c.
I hope this helps someone, because this forum has been extremely helpful to me. If some newbie can avoid getting a divorce because he doesn't have to spend 4 hours at work figuring this out like I did, I will be a happy person.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying to print the contents of a hash, but getting a reference.
by IlyaM (Parson) on Dec 17, 2001 at 02:17 UTC |