Perl
  Home arrow Perl arrow Page 3 - Perl 101 (part 7) - CGI Basics
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
PERL

Perl 101 (part 7) - CGI Basics
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway