Skip to Content
Perl Cookbook, 2nd Edition
book

Perl Cookbook, 2nd Edition

by Tom Christiansen, Nathan Torkington
August 2003
Intermediate to advanced content levelIntermediate to advanced
964 pages
23h 24m
English
O'Reilly Media, Inc.
Content preview from Perl Cookbook, 2nd Edition

Chapter 5. Hashes

Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi.

Larry Wall

Introduction

People and parts of computer programs interact in all sorts of ways. Single scalar variables are like hermits, living a solitary existence whose only meaning comes from within the individual. Arrays are like cults, where multitudes marshal themselves under the name of a charismatic leader. In the middle lies the comfortable, intimate ground of the one-to-one relationship that is the hash. (Older documentation for Perl often called hashes associative arrays, but that’s a mouthful. Other languages that support similar constructs sometimes use different terms for them; you may hear about hash tables, tables, dictionaries, mappings, or even alists, depending on the language.)

Unfortunately, this isn’t a relationship of equals. The relationship encoded in a hash is that of the genitive case or the possessive, like the word “of " in English, or like “’s”. We could encode that the boss of Nat is Tim. Hashes only give convenient ways to access values for Nat’s boss; you can’t ask whose boss Tim is. Finding the answer to that question is a recipe in this chapter.

Fortunately, hashes have their own special benefits, just like relationships. Hashes are a built-in data type in Perl. Their use reduces many complex algorithms to simple variable accesses. They are also fast and convenient to build indices and quick lookup tables.

Only use the % when referring ...

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

Perl Cookbook

Perl Cookbook

Tom Christiansen, Nathan Torkington
Programming Perl, 3rd Edition

Programming Perl, 3rd Edition

Larry Wall, Tom Christiansen, Jon Orwant
Learning Perl, 7th Edition

Learning Perl, 7th 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: 0596003137Supplemental ContentErrata Page