Hashes

A hash is an array in which you index the data with a string rather than a number. For example:

my %flags; 
$flags{start} = "MN_START"; 
$flags{end} = "MN_TERMINATE"; 
$flags{middle} = "CONNECTED";

There are a few bookkeeping details to take care of first. A hash name begins with a percent sign (%). An element of a hash is a scalar, so it begins with a dollar sign ($). Whereas simple arrays use square brackets ([]) to index the array, hashes use curly brackets ({}).

The index of a hash is called a key. In this example, the keys are start, end, and middle. The result of a hash indexed by a key is a value. In this example, the values are "MN_START", "MN_TERMINATE", and "CONNECT".

Finally, this example indexes %flags with a bare word, which ...

Get Perl for C Programmers 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.