Search⌘ K

Hashes

Explore how to declare and use hashes in Perl as associative data structures that link string keys to scalar values. Understand key concepts like the fat comma operator, hash indexing, and emptying hashes. This lesson helps you grasp practical hash usage for maintainable and idiomatic Modern Perl coding.

A hash is an aggregate data structure that associates string keys with scalar values. Just as the name of a variable corresponds to something that holds a value, a hash key refers to something that contains a value. Think of a hash like a contact list: we use the names of our friends to look up their birthdays. Other languages call hashes tables, associative arrays, dictionaries, and maps.

Hashes have two important properties: they store one scalar per unique key and they provide no specific ordering of keys. Keep that latter property in mind. Though it has always been true in Perl, it’s very, very true in Modern Perl.

Declaring hashes

Hashes use the % sigil. We declare a lexical hash with this:

my
...