REVISED: Sunday, March 3, 2013
You will learn how to use the Perl hash.
A Perl hash is a Perl array that is indexed by a string instead of a number. Hashes are like arrays except they link a key to a value. To define a hash, use the percent (%) symbol before the name.
Hashes can be key-value pair indexed using the two scalar variables $key and $value. The table below contains USA currency portrait and dollar amounts. $key is the last name of the person whose portrait is on the bill and $value is the face value of the bill in dollars.
The hash %bills is declared in bill amount order $value.
| $key | $value |
|---|---|
| Washington |
1
|
| Jefferson |
2
|
| Lincoln |
5
|
| Hamilton |
10
|
| Jackson |
20
|
| Grant |
50
|
| Franklin |
100
|
| McKinley |
500
|
| Cleveland |
1000
|
| Madison |
5000
|
| Chase |
10000
|
| Wilson | 100000 |
II. PERL HASH EXAMPLE SOURCE CODE
Copy paste the following into your text editor.
#Provides undefined value warnings.
use warnings;
#enables perl's -w flag.
use diagnostics;
#Helps you catch typos.
#Forces you to use my() function to declare all variables.
#The my() function makes the variables local to the main package.
use strict;
#stack trace
use Carp ( );
local $SIG{__WARN__} = \&Carp::confess;
#Specify the file.
my($file) = "C:/Strawberry/hash.pl";
#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";
#DEFINE HASH %bills
my(%bills) = (Washington => 1,
Jefferson => 2,
Lincoln => 5,
Hamilton => 10,
Jackson => 20,
Grant => 50,
Franklin => 100,
McKinley => 500,
Cleveland => 1000,
Madison => 5000,
Chase => 10000,
Wilson => 100000,
);
foreach my $key (sort (keys(%bills))) {
if (length($key) <= 6){
printf ("\t%s \t\t%.6s\n", $key, $bills{$key});
}
else{
printf ("\t%s \t%.6s\n", $key, $bills{$key});
}
}
#Close the file.
close FILE;
From your text editor, "File Save As" hash.pl using the path to the folder of your Perl download.
From the Perl prompt type the following:
C:\WINDOWS\system32>C:\Strawberry\hash.pl
Press Enter and the following will display on your screen:
C:\WINDOWS\system32>C:\Strawberry\hash.pl
BILLS SORTED BY PORTRAIT NAME
Chase 10000
Cleveland 1000
Franklin 100
Grant 50
Hamilton 10
Jackson 20
Jefferson 2
Lincoln 5
Madison 5000
McKinley 500
Washington 1
Wilson 100000
C:\WINDOWS\system32>
III. PERL HASH SUMMARY
The hash %bills is sorted by bill portrait last name $key.
| $key | $value |
|---|---|
| Chase |
10000
|
| Cleveland |
1000
|
| Franklin |
100
|
| Grant |
50
|
| Hamilton |
10
|
| Jackson |
20
|
| Jefferson |
2
|
| Lincoln |
5
|
| Madison |
5000
|
| McKinley |
500
|
| Washington |
1
|
| Wilson | 100000 |
The each( ) function removes the topmost key-value pair from the hash and stores them in the variables $key and $value. Each time the loop iterates, the statement ($key, $value) = each(%bills) executes and the hash pops off the top key-value pair and continues doing so until it has gone through every key-value pair in the hash. When it is done, each( ) returns false and the loop stops running.
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment