Thanks for stopping by for this seventh part in the series on Perl Lists and Hashes. Here we will discuss working with a new way to store data known as a Hash. We'll cover the basics, such as how to create them, and how to locate data within them. You'll learn to add records, remove records, and much more. If there is time, we will also cover multidimensional lists.
In our previous article we covered the remaining List::Util subroutines. We used min(list) to find the minimum numeric value in a list. We also worked with its brother, the minstr(list), which as you will recall found the smallest string value in a list. We learned that uppercase letters hold a lower value, surprisingly, then lowercase ones. We also learned to use shuffle to randomly sort our lists, and sum(list) to sum up the numeric values in our lists.
Hash...Not What It Sounds Like
If you have worked with Access or Excel, or any grid-type program, you are familiar with the concept of a hash. These aren't exactly like a hash mind you, but a similar idea. Whereas with a typical list, you refer to the values stored in them by referencing their element or index number, like so:
array[0]
with hashes you look them up with their key. When you create an element in a hash, you assign it a key value. That key, plus the value, creates a record. If that is still confusing to you, never fear: after our example it should be crystal clear.