Perl
  Home arrow Perl arrow Page 3 - Perl: Another Round with Hashes
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl: Another Round with Hashes
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-05-05

    Table of Contents:
  • Perl: Another Round with Hashes
  • Adding More Than One Record
  • Using the Delete Function
  • Storing a Deleted Value in a Variable

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    Perl: Another Round with Hashes - Using the Delete Function
    (Page 3 of 4 )

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


       · Thanks for stopping by to read another article in my series on Perl Lists and...
     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway