Skip to Content
Learning Perl, 7th Edition
book

Learning Perl, 7th Edition

by Randal L. Schwartz, brian d foy, Tom Phoenix
October 2016
Beginner
391 pages
10h 38m
English
O'Reilly Media, Inc.
Content preview from Learning Perl, 7th Edition

Chapter 6. Hashes

In this chapter, you will see a feature that makes Perl one of the world’s truly great programming languages—hashes. Although hashes are a powerful and useful feature, you may have used other powerful languages for years without ever hearing of hashes. But you’ll use hashes in nearly every Perl program you write from now on; they’re that important.

What Is a Hash?

A hash is a data structure, not unlike an array in that it can hold any number of values and retrieve them at will. But instead of indexing the values by number, as you did with arrays, you look up hash values by name. That is, the indices, called keys, aren’t numbers, but instead they are arbitrary, unique strings (see Figure 6-1).

Hash keys are strings, first of all, so instead of getting element number 3 from an array, you access the hash element named wilma, for instance.

These keys are arbitrary strings—you can use any string expression for a hash key. And they are unique strings—just as there’s only one array element numbered 3, there’s only one hash element named wilma.

Another way to think of a hash is that it’s like a barrel of data, where each piece of data has a tag attached. You can reach into the barrel and pull out any tag and see what piece of data is attached. But there’s no “first” item in the barrel; it’s just a jumble. In an array, you start with element 0, then element 1, then element 2, and so on. But in a hash there’s no fixed order, no first element. It’s just a collection of key-value ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Perl, 2nd Edition

Mastering Perl, 2nd Edition

brian d foy
Perl One-Liners

Perl One-Liners

Peteris Krumins
Learning Perl, 8th Edition

Learning Perl, 8th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming Perl, 4th Edition

Programming Perl, 4th Edition

Tom Christiansen, brian d foy, Larry Wall, Jon Orwant

Publisher Resources

ISBN: 9781491954317Errata Page