TechRepublic : A ZDNet Tech Community

Create key-value data associations by understanding the benefits of Perl hashes

Tags: Scripting languages, Contributor Melonfire, benefit, hash, apple, hashe, Perl

  • Save
  • Print
  • Recommend
  • 4

Takeaway: Get a crash course in creating Perl hashes, adding and deleting elements, building nested hashes and processing hashes in a loop.

One of Perl's most interesting data structures is the hash, which makes it possible to create key-value associations between data fragments. These hashes, although far more versatile than plain-vanilla numerically-indexed arrays, are often intimidating to the novice user. And that's where this document comes in - it provides a crash course in creating Perl hashes, adding and deleting elements, building nested hashes and processing hashes in a loop.

Defining hashes


First, what is a hash? Essentially, it's a string-indexed array, which means that instead of using numbers to access individual elements, you use string labels. Here's an example:

Listing A


#!/usr/bin/perl

# define hash
%alphabet = ('a' => 'apple',
             'b' => 'ball',
             'c' => 'cat',
             'x' => 'xylophone');

The line above creates a Perl hash with four key-value pairs. Notice the % sign before the variable name to denote that it is a hash, and the use of arrows to indicate the key-value relationships.

You could also write this as:

Listing B


#!/usr/bin/perl

# define hash
%alphabet = ('a', 'apple', 'b', 'ball', 'c', 'cat', 'x', 'xylophone');

Once you've defined a hash, you can access individual elements by name. So, for example, the following code:

Listing C


#!/usr/bin/perl

# define hash
%alphabet = ('a', 'apple', 'b', 'ball', 'c', 'cat', 'x', 'xylophone');

# access hash value
print "A is for " . $alphabet{'a'};

Would output as:

A is for apple.

To clear a hash, simply assign it an empty data set, as in the following example:

Listing D


#!/usr/bin/perl

# (re)initialize hash
%alphabet = ();

Adding, changing and deleting hash elements


You can add a new element to a hash (or alter an existing one) simply by setting a new value for the corresponding key. If the key does not already exist, it will be created. Here's an example:

Listing E


#!/usr/bin/perl

# define hash
%alphabet = ('a', 'apple', 'b', 'ball', 'c', 'cat', 'x', 'xylophone');

# add new element
$alphabet{'d'} = 'dog';

# change existing element
$alphabet{'a'} = 'arrow';

# access hash value
print "A is for " . $alphabet{'a'};

You can delete a key-value pair from a hash by using the delete() function, as below:

Listing F


#!/usr/bin/perl

# define hash
%alphabet = ('a', 'apple', 'b', 'ball', 'c', 'cat', 'x', 'xylophone');

# delete element
delete $alphabet{'x'};

Retrieving keys and values


Perl lets you extract the keys and values of a hash into separate data structures, useful if you need to reprocess them further. You can use the keys() function to return the keys of a hash as a numerically-indexed  array. Here's an example:

Listing G


#!/bin/perl

# define hash
%alphabet = ('a' => 'apple', 'b' => 'bat', 'c' => 'cat');

# get and print hash keys
@keys = keys(%alphabet);
print "@keys ";

Alternatively, you can use the values() function to get an array of only the hash values, as below:

Listing H


#!/bin/perl

# define hash
%alphabet = ('a' => 'apple', 'b' => 'bat', 'c' => 'cat');

# get and print hash values
@vals = values(%alphabet);
print "@v ";

Calculating hash size


The easiest way to calculate how large a hash is, is to use the aforementioned keys() function to extract the hash keys into an array and then retrieve the size of the array. Here's how:

Listing I


#!/bin/perl

# define hash
%alphabet = ('a' => 'apple', 'b' => 'bat', 'c' => 'cat');

# print number of hash elements
print "The hash has " . scalar(keys(%alphabet)) . " elements\n";

Processing hash elements

It's also easy to process all the elements in a hash using a while() loop. Here's a simple example:

Listing J


#!/usr/bin/perl

# define hash
%alphabet = ('a', 'apple', 'b', 'ball', 'c', 'cat', 'x', 'xylophone');

# loop over hash
while (($key, $value) = each(%alphabet) ) {
        print "$key is for $value\n";
}

Alternatively, use a for() loop with the keys() function discussed previously:

Listing K


#!/usr/bin/perl

# define hash
%alphabet = ('a', 'apple', 'b', 'ball', 'c', 'cat', 'x', 'xylophone');

# loop over hash
for $k (keys(%alphabet)) {
        print $k . " is for " . $hash{$key} . "\n";
}

Using nested hashes

Perl also lets you "nest" one hash (or array) within another hash (or array). This allows for a great deal of flexibility when formulating long and complex data structures. Here's an example:

Listing L


#!/usr/bin/perl

%movies = (
   'black' => {'hero' => 'Batman', 'villain' => 'The Penguin'},
   'red'   => [{'hero' => 'Spiderman', 'villain' => 'Green Goblin'},
               {'hero' => 'Superman', 'villain' => 'LexLuthor'}]
);

# retrieve and print values
print $movies{'black'}{'hero'} . " fights " .  $movies{'black'}{'villain'} . "\n";

print $movies{'red'}[1]{'hero'} . " fights " .  $movies{'red'}[1]{'villain'} . "\n";

This code returns:

Batman fights The Penguin
Superman fights LexLuthor

And that's about all you need to get started with Perl hashes. Have fun, and see you soon!

  • Save
  • Print
  • Recommend
  • 4

Print/View all Posts Comments on this article

Homemade hash Mark W. KaelinTechrepublic Moderator | 06/23/06
back in "high" school... fungusAmongus | 06/26/06

What do you think?

White Papers, Webcasts, and Downloads

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

SmartPlanet

Click Here