Home arrow Perl Programming arrow Page 3 - Perl: A Continuing Look at Hashes and Multidimensional Lists

How To See If A Specific Record Exists - Perl

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.

TABLE OF CONTENTS:
  1. Perl: A Continuing Look at Hashes and Multidimensional Lists
  2. Using If Statements to See if a Record Exists
  3. How To See If A Specific Record Exists
  4. Molemen...From the Second Dimension!
By: James Payne
Rating: starstarstarstarstar / 2
May 12, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: