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

Open Sesame - 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
The example above demonstrates how hashes can speed up access to specific values in the array. However, if you'd like to access each and every element of the hash in a sequential fashion, things can get hairy.which is why Perl has a few functions designed to simplify that task.

The first of these is the keys() function, which returns a list of all the keys in the specified hash as an array. Here's an example:
#!/usr/bin/perl
# define a hash
%director = ("1995" => "Mel Gibson", "1996" => "Anthony Minghella", "1997"
=> "James Cameron", "1998" => "Steven Spielberg", "1999" => "Sam Mendes");
# get the names
@year = keys(%director);
# and use them in a loop
foreach $year(@year)
{
print "And the Oscar for Best Director($year) goes to
$director{$year}\n";
}

And the output is:

And the Oscar for Best Director (1995) goes to Mel Gibson
And the Oscar for Best Director (1996) goes to Anthony Minghella
And the Oscar for Best Director (1997) goes to James Cameron
And the Oscar for Best Director (1998) goes to Steven Spielberg
And the Oscar for Best Director (1999) goes to Sam Mendes

In this case, the keys() function returns an array named @year, which looks like this:
@year = ("1995", "1996", "1997", "1998", 1999");

Once this array has been generated, the "foreach" loop is used to iterate through it and print each name-value pair. And there's a corresponding values() function, which returns.yup, you guessed it, the values from a hash.
#!/usr/bin/perl
# define a hash
%director = ("1995" => "Mel Gibson", "1996" => "Anthony Minghella", "1997"
=> "James Cameron", "1998" => "Steven Spielberg", "1999" => "Sam Mendes");
# get the names
@names = values(%director);
print "The Best Directors on the planet are: \n";
# and use them in a loop
foreach $name (@names)
{
print "$name \n";
}

And the output is:

The Best Directors on the planet are:
Mel Gibson
Anthony Minghella
James Cameron
Steven Spielberg
Sam Mendes



This article copyright Melonfire 2000. All rights reserved. {mospagebreak title=Each() Time The Lights Go Out.} Perl also has the each() function, designed to return a two-element array for each key-value pair in the hash. This comes in particularly useful when iterating through the hash, and is conceptually similar to the foreach() loop. Take a look:
#!/usr/bin/perl
# define movie hash
%film = (1995 => 'Braveheart', 1996 => 'The English Patient', 1997 =>
'Titanic', 1998 => 'Saving Private Ryan', 1999 => 'American Beauty');
# define director hash
%director = ('Braveheart' => 'Mel Gibson', 'The English Patient' =>
'Anthony Minghella', 'Titanic' => 'James Cameron', 'Saving Private Ryan' =>
'Steven Spielberg', 'American Beauty' => 'Sam Mendes');
# use loop to iterate through hash
while (($year, $filmname) = each %film)
{
print "The Oscar for Best Director($year) goes to
$director{$filmname} for $film{$year}\n";
}

In the example above, we've created two hashes, one for the movies and years, and the other linking the movies with their directors. Next, we've used the each() function to assign the name-value pairs from the first hash to two variables, $year and $filmname, which are then used to obtain the corresponding director names from the second hash.

Here's the output:

The Oscar for Best Director(1995) goes to Mel Gibson for Braveheart
The Oscar for Best Director(1996) goes to Anthony Minghella for The English
Patient
The Oscar for Best Director(1997) goes to James Cameron for Titanic
The Oscar for Best Director(1998) goes to Steven Spielberg for Saving
Private Ryan
The Oscar for Best Director(1999) goes to Sam Mendes for American Beauty

Note that Perl allows you to use scalars within the hash notation as well - $director{$filmname} above is an example of this.

And finally, the delete() function allows you to delete a pair of elements from the hash. For example, if you have the hash
%film = (1995 => 'Braveheart', 1996 => 'The English Patient', 1997 =>
'Titanic', 1998 => 'Saving Private Ryan', 1999 => 'American Beauty');

you can delete the second entry(1996) like this:
delete $film{1996};

And your hash will then look like this:
%film = (1995 => 'Braveheart', 1997 => 'Titanic', 1998 => 'Saving Private
Ryan', 1999 => 'American Beauty');



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: