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

Using If Statements to See if a 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

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 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: