Perl
  Home arrow Perl arrow Page 3 - 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 - How To See If A Specific Record Exists


    (Page 3 of 4 )

    Checking whether a hash contains any data whatsoever is good, but what if we need something a little more specific? For that we could use our good old buddy the exist() function, which, as the name implies, checks to see if a specific key exists. Here it is in action, checking whether the “Champ” is in our hash:


    #!/usr/bin/perl

    %Wrestlers=(Chump => ' Chavo Guerrero ', OldSkool=> ' Big John Stud ',

    Boof=> ' Brutus Beefcake ');

    unless(exists($Wrestlers{Champ}))

    { print "Where is the champ?"}

    Since the “Champ” key does not exist, the print function is triggered, resulting in:

      Where is the champ?

    And of course, just as before, we can use if to check if the value is in the hash and print a result if it is:


    #!/usr/bin/perl

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

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

    if(exists($Wrestlers{Champ}))

    { print "I see the champ!"}

    Giving us a very enthusiastic:

      I see the champ!

    We can also use an if-else to print something out if the value does not exist, or use the if statement and the unless to achieve the same effect. Here they both are in the following examples:


    #!/usr/bin/perl

    %Wrestlers=(Chump => ' Chavo Guerrero ', OldSkool=> ' Big John Stud ',

    Boof=> ' Brutus Beefcake ');

    if(exists($Wrestlers{Champ}))

    { print "I see the champ!"}

    else {print "Has anyone seen the champ?"}

    or, the same result but written differently:


    #!/usr/bin/perl

    %Wrestlers=(Chump => ' Chavo Guerrero ', OldSkool=> ' Big John Stud ',

    Boof=> ' Brutus Beefcake ');

    if(exists($Wrestlers{Champ}))

    { print "I see the champ!"}

    unless(exists($Wrestlers{Champ}))

    { print "Has anyone seen the champ?"}

    Both of these would give us the same result:

      Has anyone seen the champ?

    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