Home arrow Perl Programming arrow Page 4 - Hash Functions

Hash Examples - Perl

In this second part of a two-part series on hashes in Perl, you'll learn about hash functions and hashes in different contexts. This article is excerpted from chapter five of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Hash Functions
  2. Hash in Scalar Context
  3. Hash Functions
  4. Hash Examples
  5. Counting Things
By: Apress Publishing
Rating: starstarstarstarstar / 1
May 06, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Hashes are very useful variables and there are many uses for them. Here are a few examples of using hashes to solve common problems.

Creating Readable Variables

The most basic use of a hash is to be able to index into a variable to obtain information using a readable string which is far more user-friendly than using a numeric index as we would with an array. For instance, this program shows that we can create a record of strings representing RGB colors that one might find in an HTML page:

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

use strict;

my %colors = (
   red    => '#FF0000',
   green  => '#00FF00',
   blue   => '#0000FF',
   white  => '#FFFFFF',
   black  => '#000000',
  
purple => '#520063'
);

print "Red is:    $colors{red}\n";
print "Blue is:   $colors{blue}\n";
print "Purple is: $colors{purple}\n";

Notice how the information in the hash is laid out in such a way that it is readable by human beings. It is easy to see that the RGB string for “red” is “#FF0000” and indexing into the hash is the human-friendly$colors{red}.

Executing this code produces the following:

$ perl colors.pl
Red is:    #FF0000
Blue is:   #0000FF
Purple is: #520063
$

“Reversing” Information

Recall the hash we created earlier in this chapter that was a collection of people and where they lived:

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

If you need to turn this hash around to look up people by where they live, you can use a hash in list context that produces a list of key/value pairs, reverse the list with thereverse()function, and then assign it to a new hash.

%who = reverse %where;

Be careful though—if you have two values that are the same, then converting them to keys means that one will be lost. Remember that keys must be unique.

Here is a program illustrating reversing a hash:

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

use strict;

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

my %who = reverse %where;

foreach (keys %who) {
    print "in $_ lives $who{$_}\n";
}

Executing this code produces the following:

$ perl reverse.pl
in Oregon lives Samantha
in Exeter lives Lucy
in Reading lives Ian
in Dallas lives Gary
$

After we assigned to%who, we created a hash indexed by the location producing the name that is the direct opposite of%where, which was indexed by name to produce the location.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- 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: