Perl
  Home arrow Perl arrow Page 2 - 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 - Adding More Than One Record
    (Page 2 of 4 )

    There are several ways to add multiple records to a hash. Here, we are going to add Brutus “the Barber” Beefcake to our already created hash:


    #!/usr/bin/perl

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

    $New=" Big John Stud ";

    $Boof=" Brutus Beefcake ";

    print values(%Wrestlers);

    print "\n\n";

    $Wrestlers{OldSkool}=$New;

    $Wrestlers{Beef}=$Boof;

    print values(%Wrestlers);

    Note that each time, $Wrestlers holds our values. When we print this code we get the following result:

      Brutus Beefcake Big John Stud CM Punk Chavo Guerrero

    Replacing a Record in a Hash

    Replacing a record in a hash with another record is pretty similar to adding a record. In our previous examples we stated that CM Punk was the champ (well we assigned his value to the champ key at any rate). But what happens if CM dies in a horrible car accident and the title is given to someone else? Here is how we would replace the value in the key 'champ':


    #!/usr/bin/perl

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

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

    $New=" The Nature Boy Ric Flair ";

    print values(%Wrestlers);

    print "\n\n";

    $Wrestlers{Champ}=$New;

    print values(%Wrestlers);

    Here, the %Wrestlers hash has been created and assigned a bunch of key-pair values. Next, we create a variable named $New to hold a given value. Next we print out the values of our %Wrestlers hash, then assign the key “Champ” a new value, said value being the one stored in $New. Finally, we print out the value of %Wrestlers once more to confirm that the replace took place.

    This works because Perl sees the “Champ” key and searches in the hash for it. If it finds it, it replaces its value with the one you specify. If not, it adds it to the hash.

    You can achieve the same without variables of course:


    #!/usr/bin/perl

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

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

    print values(%Wrestlers);

    print "\n\n";

    $Wrestlers{Champ}="The Nature Boy Ric Flair";

    print values(%Wrestlers);

    Giving us the same result as above:

      Brutus Beefcake Big John Stud CM Punk Chavo Guerrero

      Brutus Beefcake Big John Stud The Nature Boy Ric Flair Chavo Guerrero

    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 4 hosted by Hostway