Perl
  Home arrow Perl arrow Perl: A Continuing Look at Hashes and ...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: A Continuing Look at Hashes and Multidimensional Lists
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-05-12

    Table of Contents:
  • Perl: A Continuing Look at Hashes and Multidimensional Lists
  • Using If Statements to See if a Record Exists
  • How To See If A Specific Record Exists
  • Molemen...From the Second Dimension!

  • 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


    Perl: A Continuing Look at Hashes and Multidimensional Lists


    (Page 1 of 4 )

    Welcome to the ninth installment in our look at working with hashes and lists in Perl. In our previous article we learned how to add records to a hash, replace them, and delete the values inside of them. In this article we will learn how to check whether records reside within our hashes, write the data within a hash to a file, and create multidimensional lists.

    Has Anyone Seen My Records?

    Sometimes it is necessary to check to see if a hash holds any values. There are many ways to do this, such as printing our the entire hash, but another method is to use the "unless" function and "if" statement. In the following code we use unless to see if there are any values in our hash. If so, nothing happens. If not, we get some printed text:


    #!/usr/bin/perl

    %Wrestlers=(Champ=> ' CM Punk ', Chump => ' Chavo Guerrero ',

    OldSkool=> ' Big John Stud ', Boof=> ' Brutus Beefcake ');

    unless(%Wrestlers) { print "Hey, have you guys seen my records?"}

    In the above code we assigned some key-pair values to our %Wrestlers hash. We then used a statement that basically says, "unless there are values in %Wrestlers, print some text." Since there are values in %Wrestlers, in this instance nothing happens.

    Now let's modify the code a little by removing the values in our hash:


    #!/usr/bin/perl

    %Wrestlers=();

    unless(%Wrestlers) { print "Hey, have you guys seen my records?"}

    When we run the program this time, the print statement is triggered because there are indeed no values in %Wrestlers. Here is the result:

      Hey, have you guys seen my records?

    More Perl Articles
    More By James Payne


       · Thanks for stopping by to read another installment of my series on Lists and Hashes....
     

       

    PERL ARTICLES

    - 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
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util





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