Home arrow Perl Programming arrow Page 3 - Perl: Another Round with Hashes

Using the Delete Function - Perl

Welcome to the eighth episode in our series on Perl Lists and Hashes. In this article we will discuss how to add a record to a hash, as well as how to remove a record from one. We will also cover how to check to see if a record already exists in our hash, how to write the data contained in one to a file, and quite possibly go over the basics of multidimensional lists.

TABLE OF CONTENTS:
  1. Perl: Another Round with Hashes
  2. Adding More Than One Record
  3. Using the Delete Function
  4. Storing a Deleted Value in a Variable
By: James Payne
Rating: starstarstarstarstar / 2
May 05, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

But what if we don't want to replace CM Punk just yet? Perhaps they are going to decide the new champ via a Wrestlemania contest or something. In this case, we may wish to delete the record for now and add it later. Deleting a record in a hash is a simple matter. To do so, we simply use the conveniently built-in delete() function:


#!/usr/bin/perl

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

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

print values(%Wrestlers);

print "\n\n";

delete($Wrestlers{Champ});

print values(%Wrestlers);

Here, once more values are assigned to a hash and printed out. Then we use delete() to remove a record by specifying which record to remove. In this instance we removed the value in “Champ.” When we print this out, we get the following result:

  Brutus Beefcake Big John Stud CM Punk Chavo Guerrero

  Brutus Beefcake Big John Stud Chavo Guerrero

Now let's say that CM Punk and Chavo Guerrero happened to be riding in the same car and both of them died. If we wanted to delete multiple records from our hash, we could do so in the following manner:


#!/usr/bin/perl

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

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

print values(%Wrestlers);

print "\n\n";

delete(@Wrestlers{Champ,Chump});

print values(%Wrestlers);

As you can see, this is very similar code. The only differences really are the addition of the record Chump, separated by a comma, and the change of the variable $Wrestlers to a list, or @Wrestlers, since we are removing more than one record.

Here are our results:

  Brutus Beefcake Big John Stud CM Punk Chavo Guerrero

  Brutus Beefcake Big John Stud

The wrestling world sure is shrinking.



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