Home arrow Perl Programming arrow Page 3 - Hashes

Working with Hash Values - Perl

Hashes in Perl aren't really that hard to understand; they're not that much harder than understanding how a phone book works. This article introduces you to hashes, what they do, and how to use them. It is excerpted from chapter five of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Hashes
  2. Creating a Hash
  3. Working with Hash Values
  4. Adding, Changing, and Removing Elements
By: Apress Publishing
Rating: starstarstarstarstar / 3
April 29, 2010

print this article



To look up a value in a hash, we use something similar to the index notation for arrays. However, instead of locating elements by number, we’re now locating them by name, and instead of using square brackets, we use curly braces.

Here’s a simple example of looking up details in a hash:

#!/usr/bin/perl -w
# hash.pl

use strict;

my $who = "Ian";

my %where = (
        Gary     => "Dallas",
        Lucy     => "Exeter",
        Ian      => "Reading",
        Samantha => "Oregon"

print "Gary lives in ", $where{Gary}, "\n"; print "$who lives in $where{$who}\n";

$ perl hash.pl
Gary lives in Dallas
Ian lives in Reading

The first thing we do in this program is set up our main hash, which tells us where people live.

my %where = (
        Gary     => "Dallas",
        Lucy     => "Exeter",
        Ian      => "Reading",
        Samantha => "Oregon"

Like scalars and arrays, hash variables must be declared withmy()when using strict.

Now we can look up an entry in our hashes—we’ll ask “Where does Gary live?”

print "Gary lives in ", $where{Gary}, "\n";

This is almost identical to looking up an array element, except for using curly braces instead of square brackets and the fact that we are now allowed to use strings to index our elements. Notice that the keyGaryis not quoted within the curly braces. If the key contains no whitespace characters, it is assumed quoted within the curly braces. If the key does contain whitespace characters, then we will have to quote it.

The next line is

print "$who lives in $where{$who}\n";

Just as with array elements, we need not use a literal to index the element—we can look up using a variable as well.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: