Defining and Using Hashes

Arrays and hashes can be created and used in many of the same ways. Hashes, however, do have some peculiarities and extra features that result from the way data is stored in a hash. For example, when you put data into a hash, you'll have to keep track of two scalars for each element (the key and the value). Because hashes are unordered, you'll have to do extra work to extract values from the hash, as well as to sort them and print them. In addition, hashes perform differently than arrays in a scalar context. Read on to learn about all these things.

List Syntax and Hashes

List syntax—enclosing the elements of a list inside parentheses, separated by commas—works to create a hash just as well as it does an array. Just stick ...

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.