I'm sure there's an answer to this question, but I don't know the right term to use to find it. If I search for hash I get a lot of info about hashes but nothing about this:
#!/usr/local/bin/perl
%c={A=>'0',B=>'0',C=>'0',D=>'0'};
foreach $key (keys %c) { print "$key => $c{$key}\n"; }
Output is:
HASH(0x1b9550c) =>
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.