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

Perl: A Continuing Look at Hashes and Multidimensional Lists

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

Has Anyone Seen My Records?

Sometimes it is necessary to check to see if a hash holds any values. There are many ways to do this, such as printing our the entire hash, but another method is to use the "unless" function and "if" statement. In the following code we use unless to see if there are any values in our hash. If so, nothing happens. If not, we get some printed text:


#!/usr/bin/perl

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

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

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

In the above code we assigned some key-pair values to our %Wrestlers hash. We then used a statement that basically says, "unless there are values in %Wrestlers, print some text." Since there are values in %Wrestlers, in this instance nothing happens.

Now let's modify the code a little by removing the values in our hash:


#!/usr/bin/perl

%Wrestlers=();

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

When we run the program this time, the print statement is triggered because there are indeed no values in %Wrestlers. Here is the result:

  Hey, have you guys seen my records?



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