SunQuest
 
       Perl
  Home arrow Perl arrow Page 3 - 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 
Actuate Whitepapers 
VeriSign Whitepapers 
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: 4 stars4 stars4 stars4 stars4 stars / 2
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl Hashes - Printing from a Hash


    (Page 3 of 5 )

    There are several print methods that you can use with hashes. The first we will learn about is printing individual values from a hash. Here, we will use our previous list of different types of brownies to print from:


    #!/usr/bin/perl

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

    ');

    print $Brownies{1};

    You will note that we use $Brownies in our print statement instead of %Brownies. This is because we are only printing a single value, and thus it becomes a variable. The result of this is:

      Chocolate

    If we wanted to print more than one value from the hash, we would change the $Brownies variable to @Brownies, since there will be more than one value in it. Here is how we return two or more values:


    #!/usr/bin/perl

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

    ');

    print @Brownies{1,2};

    This simplistic code returns the following:

      Chocolate Fudge

    Note that the keys that refer to your values don't have to be numbers. Here, we will use some text instead:


    #!/usr/bin/perl

    %HowItIs = (Dumb,'You ', Fat,'YoMama ',UglyGenius,'James Payne

    ',Nerd,'PHP Programmers');

    print @HowItIs{Dumb,Fat,Nerd};

    This code assigns the value “You” to the key Dumb, “YoMama” to Fat, and so forth. When we run this program, it prints the following:

      You YoMama PHP Programmers

    Another thing to remember is that your keys may not equal the same value, but the value inside your keys can. Let's say that in addition to being an ugly genius, I am also a nerd. Here is how I could assign myself to two keys:


    #!/usr/bin/perl

    %HowItIs = (Dumb,'You ', Fat,'YoMama ',UglyGenius,'James Payne

    ',Nerd,'James Payne');

    print @HowItIs{UglyGenius,Nerd};

    The result is of course:

      James Payne James Payne

    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

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





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