1. Home
  2. Computing & Technology
  3. Perl

Beginning Perl Tutorial - Data Types: Hashes
Playing With Hashes

by Kirk Brown
for About.com

What else can you do with hashes? Just like with scalars and lists, there are built-in functions that allow you to perform various operations and countless ways to manipulate the data. Let's take a look at using a hash to keep a simple data structure and access the data in some useful ways. Here is the full code we'll be working with:
#!/usr/local/bin/perl
%myGrades = (
'name' => 'Bob',
'test1' => 89,
'test2' => 76,
'test3' => 0);
$average = ($myGrades{'test1'} + $myGrades{'test2'} + $myGrades{'test3'}) / 3;
$average = int($average + .5);
print "$myGrades{'name'}'s Current Average: $average\n";
$myGrades{'test3'} = 100;
$average = ($myGrades{'test1'} + $myGrades{'test2'} + $myGrades{'test3'}) / 3;
$average = int($average + .5);
print "$myGrades{'name'}'s Corrected Average: $average\n";
When you run the program you should see:
Bob's Current Average: 55
Bob's Corrected Average: 88
Let's take a look at each part of this simple program now, starting with the initialization of the hash itself:
%myGrades = (
'name' => 'Bob',
'test1' => 89,
'test2' => 76,
'test3' => 0);
We're creating a simple data structure out of a hash that contains Bob's name, and his score on three tests that he took in his class at school. You can see that the data is both a string, and a small collection of integers (the test scores), and we're using the => symbol to match the key / value pairs for this particular hash.

Next we will perform a simple mathematical operation on the test scores and find Bob's current average.

$average = ($myGrades{'test1'} + $myGrades{'test2'} + $myGrades{'test3'}) / 3;
$average = int($average + .5);
print "$myGrades{'name'}'s Current Average: $average\n";
What we do in the first line is simply add the three test scores by accessing them as scalars with their keys, then divide the total by three. The next line uses the perl function int to force the answer into a plain integer form so that we don't get any decimals in the answer. Adding .5 to the value has the effect of rounding the number up or down depending on the decimal.

55 seems like an awfully low average for Bob, but when we look at the hash, we'll see that test3 has a value of zero. Turns out we didn't know Bob's score for that particular test, so we'll need to add it in later.

$myGrades{'test3'} = 100;
This will set the hash value 'test3' to 100, meaning Bob scored perfect on his final test. Now let's re-average the values and see what his corrected average is using the exact same method.
$average = ($myGrades{'test1'} + $myGrades{'test2'} + $myGrades{'test3'}) / 3;
$average = int($average + .5);
print "$myGrades{'name'}'s Corrected Average: $average\n";
Of course, upon re-calculating the value, we see that Bob's final grade average is actually 88. The use of a hash in this case has created a data structure that makes the program mush more readable, and we got to see the int function in action. There are many more functions to explore in the core library, but for now just remember the following points:
  1. A hash is the third of 3 basic data types in Perl.
  2. A hash is an unordered set of virtual boxes that each holds a single piece of data.
  3. Each value in a hash is identified by a special scalar key, creating key / value pairs.
Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Beginning Perl Tutorial - Data Types: Hashes>

©2009 About.com, a part of The New York Times Company.

All rights reserved.