Going Deeper

Hashes have a lot of similarities to arrays and lists, so actually we don't need to go much deeper for this lesson. Hashes use one other function that might be useful: each.

The keys function takes a hash as an argument and returns a list of the hash's keys. The values function does the same thing with the values in the hash. The each function does both: with a hash as an argument, it returns a list of two elements—the first a key and the second a value. Calling each multiple times works through all the hash's elements. As with all hash elements, the pairs you get out of the hash are in some undetermined order. After all the elements have been exhausted, each returns an empty list (). You can learn more about each in the perlfunc ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.