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.
If you are familiar with other programming languages, then you will know that a hash is similar to an associative array. Before we create our first hash, there are a few rules you must learn. The first one is the naming convention. Hashes are named like other lists, except that they start with a percent sign (%) rather than the ampersand (@). When you refer to a key you use the curly braces {}, as opposed to parentheses as in regular lists. When you create the hashes, you use parentheses. Okay, enough blabbering; let's create our first hash:
Not quite what we expected right? You will note that hashes are not ordered lists and will not necessarily print in the order you enter them. Also, you will notice that the key gets printed along with the value when we print in this manner. We will learn in a short bit how to print only the values when we want to print the entire hash.