Perl
  Home arrow Perl arrow Page 2 - Perl: A Continuing Look at Hashes and Multidimensional Lists
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: A Continuing Look at Hashes and Multidimensional Lists
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    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:
      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: A Continuing Look at Hashes and Multidimensional Lists - Using If Statements to See if a Record Exists
    ( Page 2 of 4 )

    You can use the if statement in a manner that is opposite to the way the unless function was used. In this situation, the print function will execute if there are values in the %Wrestlers hash:


    #!/usr/bin/perl

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

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

    if(%Wrestlers) { print "Hey, Look at all my records!"}

    Giving us the result:

      Hey, Look at all my records!

    If there were no records in %Wrestlers, then nothing would have happened.

    To fix this, we can do one of two things. The most obvious would be to use an "else" clause with our if statement, like so:


    #!/usr/bin/perl

    %Wrestlers=();

    if(%Wrestlers) { print "Hey, Look at all my records!"}

    else {print "Hey, who stole all my records?!?"}

    Now if the hash contains data it will print one thing, and if it doesn't, it will print another. Since there is no data in our hash, this is the result:

      Hey, who stole all my records?

    The second way to get a result whether or not there is data is to use the if statement and unless function together, like so:


    #!/usr/bin/perl

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

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

    if(%Wrestlers) { print "Hey, Look at all my records!"}

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

    Which works in the same manner as our previous example. Which you use is up to you.



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek