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.

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?

{mospagebreak title=Using If Statements to See if a Record Exists}

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.

{mospagebreak title=How To See If A Specific Record Exists}

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?

{mospagebreak title=Molemen…From the Second Dimension!}

Two-dimensional lists are lists that have other lists as their items. Go ahead. Grab that mop and clean up the mess I just made by making your head explode. In the next few examples, we are going to pretend that we have a database of lame superheroes that just never made it. Each record will consist of the following:


  • Secret Lair

  • Name

  • Super power

  • Super weakness

Let’s go ahead and create our first two-dimensional array:


#!/usr/bin/perl

@StuporHeroes = (

[' Mount Tittikanaka ', ' Man-Girl ', ' Is a good listener ', ' Has

Man-Boobs '],

['Trailer Park', 'Deaf Leapard', 'Has a super sonic guitar', 'Is deaf

and has one arm making him unable to play his guitar']

);

In the above example, we created a two-dimensional list named @StuporHeroes. You will note that even though our list has an added dimension, we still use the @ symbol before the name, just as with our normal lists. We then encase the values in our two-dimensional list with square brackets, instead of parentheses as per normal lists. Each piece of data or “cell” is separated by a comma. Each row of data is placed between a set of parentheses, ending in a semi-colon, as shown above.

If this seems a little confusing at the moment, that is all right. When I explain them in the next section everything will become crystal clear.

Better Understanding the Two-Dimensional List

The best way to think of a two-dimensional list is to imagine an Excel sheet. I used that same analogy with hashes, but really it applies more to this storage device. For instance, in a spreadsheet you enter data into a grid, like the one shown below:



As you can see, the value Spiderman is located at row 3, column A. Batman is row 5, column A, Thanos is row 8, column B, and so forth. This is how Perl sees your record. In our sample code above for instance, we assigned our lists values like this:

@StuporHeroes = (

[' Mount Tittikanaka ', ' Man-Girl ', ' Is a good listener ', ' Has

Man-Boobs '],

['Trailer Park', 'Deaf Leapard', 'Has a super sonic guitar', 'Is deaf

and has one arm making him unable to play his guitar']

);

Our first row contains all of the following data: “Mount Tittikanaka”, “Man-Girl”, “Is a good listener” and “Has man boobs”. Note that with lists, element numbers start at 0. All of these elements have a row number of zero. However there column number differs. If I wanted to call on “Mount Tittikanaka” for instance, I would search for the value in row 0, column 0. If I wanted “Man-Girl”, I would search in row 0, column 1, and so forth.

The second row contains the values: “Trailer Park”, “Deaf Leapard”, “Has a super sonic guitar”, and “Is deaf and has one arm making him unable to play his guitar”. If I wanted to refer to “Trailer Park” I would point to row 1, column 0. “Deaf Leopard” is located at row 1 and column 1. Always keep in mind that elements start at 0 (as do our rows and columns).

Well that’s it for this article. In our next episode we will learn to print from our extra-dimensional lists, and figure out other ways to manipulate the data contained within them. So be sure to stop back often!

Till then…

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye