You're running into a common difficulty for newer programmers. Here are some good rules of thumb to keep in mind:
- Variable starts with a $: You're asking for a "scalar" value.
- Variable starts with a @: You're asking for an array of scalars.
- Variable starts with a %: It's a hash.
- Square braces - [ and ] - with the variable often indicates that you're accessing an array element.
- Curly braces - { and } - with the variable names means you're trying to access a hash element.
That's not exactly comprehensive, but it usually holds. For example, when you had the following line:
$secretword=$words{$name};
The curly braces should indicated that you were expecting "words" to be a hash. So, when you have a problem, you go back and look at how words is defined. In this case, it's declared as an array with the
@ symbol.
If you had use strict at the top of your program, you would have received a fatal error for trying to access a "words" hash that was undeclared. Actually, you would have gotten errors earlier when you tried to assign elements to the @words array. I know that's not covered right away in the book you are using, but it's a good practice to get into (along with using warnings, the -w switch). Using strict and warnings will save you many hours of debugging and, in this case, an error would have occured long before it did, thus allowing you to have a better idea what's actually causing the error.
Unfortunately, the above information is a very simplistic treatment of the issues, but it's some good stuff to always keep in mind when you are first starting out.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
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.