So, when we last left off, I left you lost in a tumultuous sea of data types, variables, and strings. Fortunately for you, you have the black Perl, the world's fastest ship and a magical compass to point you in the right direction. So let's prepare to sail, er, study, and mind the shoals.
Writing, apparently, is hungry work. Or maybe I am just a greedy pig. For some reason, this next topic makes me think of food...hashes, hash browns...mmm, yummy.
Anyway, as I was saying, the third data type is Hashes. Hashes are like lists, but they aren't in order. Instead, they are indexed by a second set of scalars called keys. This might not make sense yet, but bear with me; it will.
Observe the following masterful code written by yours truly:
Please note several things. Because we used print to print the whole hash, it printed every value. Second, you will notice that the naming convention for hashes is the same as other data types, with the exception that hashes begin with a % symbol.
So this is where Hashes begin to make sense. Let's say that I want to print only YoMama's weight from that hash. I would do so in the following manner:
In the above code, the weight 900 is printed because we used the index key, YoMama. Instead of lining everything up in order, Hashes use the first scalar as an index key, and the second as the data, and so forth. If I wanted to print the weight of James (or 400) I would have called James instead. Simple?
Again, you will note that when I printed out the single result, I changed to the $ symbol, because again, I am printing only scalars.
To make it a smidge more clear, you could also write the above hash this way: