If you understand arrays, hashes aren't really that different. Suppose you have an array called @race_results, containing an ordered list of the names of people who finished a race. Ignoring the 0th element to make things convenient, you could associate the position in the array with the name as follows:
1 -> Bob
2 -> Joe
3 -> Sunny
4 -> Hannah
It follows, if you want to find out who came in first, you would use the code
$race_results[1].
If you follow that, then hashes aren't too difficult. The only effective difference is that they're not accessed by number. There's no particular order. Instead, it's like a dictionary, where you look up a definition by the word. You might create a hash called %race_results like this:
my %race_results = (
first => 'Bob'
second => 'Joe',
third => 'Sunny',
fourth => 'Hannah',
);
To find out who came in first, say
$race_results{first};.
Think of it like an array, where you use strings instead of numbers, and you'll have it. (It's not completely accurate under the hood, but that's how I first approached it, and it's a decent mental model.)
Use them whenever you have one string to associate with another, and you want to be able to look something up as you would in an encyclopedia or a dictionary. If you can look at the world in terms of indexes, you'll have your keys.
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.