Perl
  Home arrow Perl arrow 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 
eWeek
 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Perl: Another Round with Hashes
    (Page 1 of 4 )

    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.

    In our previous issue we learned the basics of hashes -- such as how to create them, print individual values from them and print only the value. We also learned a way to print the keys. Additionally, we worked with some alternative methods to create hashes and assign values to them, such as using the => operator and using other hashes as their foundation. While we were at it, we learned to create variables and lists out of the elements within a hash.

    Want Some Hash with that Shake?

    Adding a value to a hash that already exists is similar to adding a value to a previously created list. In the following example, we begin by creating a hash name %Wrestler that will hold two key-value pairs, representing the Champ, CM Punk, and the Chump, Chavo Guerrero. We will then add a third value, 'The Miz', with the key, Dumb. Here it is in code:


    #!/usr/bin/perl

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

    print values(%Wrestlers);

    print "\n\n";

    $Wrestlers{Dumb}=' The Miz ';

    print values(%Wrestlers);

    When we run this program, we get the following result:

      CM Punk Chavo Guerrero

      CM Punk The Miz Chavo Guerrero

    Since we are only adding one value and key, we used the $Wrestlers variable and enclosed our key in curly braces {Dumb}. We then told the program to assign the value 'The Miz' to the key and assigned the $Wrestlers variable to our hash. Note that the name $Wrestlers matches the name of the hash, %Wrestlers. If they differ, this program will not work as we intended.

    We can also add a value to a hash by using a variable. Let's say we have a variable that holds the name of a wrestler. Here is how we add him to the %Wrestler hash:


    #!/usr/bin/perl

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

    $New="Big John Stud";

    print values(%Wrestlers);

    print "\n\n";

    $Wrestlers{OldSkool}=$New;

    print values(%Wrestlers);

    Note that we had to assign a key for our variable (OldSkool). Without this, the code would not have worked properly, as Perl would not have known how to reference Big John Stud.

    This program gives us the result:

      CM Punk Chavo Guerrero

      Big John Stud CM Punk Chavo Guerrero

    If you want to see how the keys to verify that the new additions were stored properly, you can do so like this:


    #!/usr/bin/perl

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

    $New="Big John Stud";

    print values(%Wrestlers);

    print "\n\n";

    $Wrestlers{OldSkool}=$New;

    @All = keys(%Wrestlers);

    foreach $Stuff(@All)

    {print "The key for $Wrestlers{$Stuff} is $Stuffn"};

    Giving us the result:

      The key for Big John Stud is OldSkool

      The key for CM Punk is Champ

      The key for Chavo Guerrero is Chump

    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: 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
    - Perl: Releasing Your Inner Textuality




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