Perl
  Home arrow Perl arrow Page 2 - Perl Hashes
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 Hashes
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-04-28

    Table of Contents:
  • Perl Hashes
  • Creating A Hash
  • Printing from a Hash
  • Printing All the Values or Keys in a Hash
  • Alternative Methods for Creating Hashes

  • 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


    Perl Hashes - Creating A Hash


    (Page 2 of 5 )

    If you are familiar with other programming languages, then you will know that a hash is similar to an associative array. Before we create our first hash, there are a few rules you must learn. The first one is the naming convention. Hashes are named like other lists, except that they start with a percent sign (%) rather than the ampersand (@). When you refer to a key you use the curly braces {}, as opposed to parentheses as in regular lists. When you create the hashes, you use parentheses. Okay, enough blabbering; let's create our first hash:


    #!/usr/bin/perl

    %Brownies = (1,'Chocolate', 2,'Fudge',3,'Vanilla',4,'Peanut Butter');

    In the above example we created four key-value pairs. If we printed out the hash, this is how we would do so, and also, the result:


    #!/usr/bin/perl

    %Brownies = (1,'Chocolate', 2,'Fudge',3,'Vanilla',4,'Peanut Butter');

    print %Brownies;

    Which prints:

      4Peanut Butter1Chocolate3Vanilla2Fudge

    Not quite what we expected right? You will note that hashes are not ordered lists and will not necessarily print in the order you enter them. Also, you will notice that the key gets printed along with the value when we print in this manner. We will learn in a short bit how to print only the values when we want to print the entire hash.

    More Perl Articles
    More By James Payne


       · Just a small correction, at the beginning where it says "... uppercase letters hold...
       · Hey,Thanks for catching that Pedro. Glad you enjoyed the article.
       · Thanks for stopping by to read another article in my series on Perl Lists and...
     

       

    PERL ARTICLES

    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util





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