Home arrow Perl Programming arrow Page 2 - Perl 101 (part 7) - CGI Basics

Meet Donald Duck - Perl

After spending the past few months understanding Perl'svariables, operators and functions, it's time to start writing Perlprograms for the Web. This week, Perl 101 introduces you to the basics ofCGI scripting, and also teaches you how to use a new type of Perl variable,the hash.

TABLE OF CONTENTS:
  1. Perl 101 (part 7) - CGI Basics
  2. Meet Donald Duck
  3. Open Sesame
  4. Perl And CGI
  5. A Cure For Low Self-Esteem
  6. GETting Your Form To Work
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 4
September 25, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

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: