Perl
  Home arrow Perl arrow Page 3 - Perl 101 (part 7) - CGI Basics
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl 101 (part 7) - CGI Basics
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2000-09-25


    Table of Contents:
  • Perl 101 (part 7) - CGI Basics
  • Meet Donald Duck
  • Open Sesame
  • Perl And CGI
  • A Cure For Low Self-Esteem
  • GETting Your Form To Work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101 (part 7) - CGI Basics - Open Sesame
    ( Page 3 of 6 )

    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 Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek