Perl
  Home arrow Perl arrow Page 2 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 - 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


       · 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

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT