Perl Programming Page 2 - Perl 101 (part 7) - CGI Basics |
So far, you've used two different types of Perl variables - the scalar and the array. However, unbeknownst to you, Perl also comes with a third type of variable - the hash. One of the significant features of an array is that array values can only be accessed via a numerical index. This implies that if you need to access an element of the array, you need to first know its exact location in the array. Since this can get complicated with large arrays, Perl offers you a simpler way to access array values, using easy-to-remember "keywords" or "keys". Thus, a hashes is a species of Perl variable which allows you to define an array of key-value pairs. Take a look:
%myhero = ("fname" => "Donald", "lname" => "Duck");
The Perl statement above will create a hash named "myhero", which consists of two name-value pairs. The first key is "fname", and it points to the value "Donald", while the second is "lname" and it points to "Duck". You can also write the statement above like this:
%myhero = ("fname", "Donald", "lname", "Duck");
Accessing the elements of a hash is equally simple - in the example above, the notation
$myhero{"fname"}
will return the first value of the hash ("Donald"), while
$myhero{"lname"}
will return the second value ("Duck"). The rules following hash names are the same as those for scalar and array variables - a hash name begins with a % symbol, followed by an alphabetic character, which may be followed by one or more numbers or letters. Hashes also have their own "space" in Perl.so the variables $duck, @duck and %duck are not treated as one and the same. This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=Heroes Of The Silver Screen} Here's a simple program that demonstrates how hashes can be used:
#!/usr/bin/perl
# define a hash
%director = ("1995" => "Mel Gibson", "1996" => "Anthony Minghella", "1997"
=> "James Cameron", "1998" => "Steven Spielberg", "1999" => "Sam Mendes");
# print
print "The Best Director Oscar in 1995 went to $director{1995}\n";
print "The Best Director Oscar in 1996 went to $director{1996}\n";
print "The Best Director Oscar in 1997 went to $director{1997}\n";
print "The Best Director Oscar in 1998 went to $director{1998}\n";
print "The Best Director Oscar in 1999 went to $director{1999}\n";
And here's the output: The Best Director Oscar in 1995 went to Mel Gibson The Best Director Oscar in 1996 went to Anthony Minghella The Best Director Oscar in 1997 went to James Cameron The Best Director Oscar in 1998 went to Steven Spielberg The Best Director Oscar in 1999 went to Sam Mendes In the example above, a hash has been used to store a bunch of name-value pairs, and a print() function has been used to display them. You can also use the alternate hash notation if you prefer.
# define a hash
%director = ("1995", "Mel Gibson", "1996", "Anthony Minghella", "1997",
"James Cameron", "1998", "Steven Spielberg", "1999", "Sam Mendes");
This article copyright Melonfire 2000. All rights reserved.
blog comments powered by Disqus |