Perl
  Home arrow Perl arrow Page 5 - Perl Hashes
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 6
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - Alternative Methods for Creating Hashes
    ( Page 5 of 5 )

    There are other ways to create hashes as well. In this next example we will use one of them. Note though that this only works for Perl 5 and later. Here, we will use the => operator to assign values:


    #!/usr/bin/perl

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

    ',Nerd=>'James Payne');

    print $HowItIs{Dumb};

    This prints out:

      You

    There really is no difference between this and the original method as far as I know. Some people say this way is easier to read, but it is really up to personal preference.

    You could also create an empty hash if you wanted, and assign values to it later:


    #!/usr/bin/perl

    %HowItIs = ();

    We'll learn how to add to it in a bit.

    What if we want to create a hash from another hash? That too is a simple process:


    #!/usr/bin/perl

    %HowItIs = (Dumb=>'You ', Fat=>'YoMama ',UglyGenius=>'James Payne ',Nerd=>'James Payne ');

    %HowItWas = %HowItIs;

    print values(%HowItWas);

    print "\n\n";

    print keys(%HowItWas);

    In the above example, we create our %HowItIs hash and assign it some values. Next, we create another hash named %HowItWas, and add the values from %HowItIs to it. We then print out first the values, then the keys to verify that it worked. The result:

      James Payne James Payne YoMama You

      UglyGeniusNerdFatDumb

    If we only wanted to add a few of the values to our newly created hash, we could do so in the following manner:


    #!/usr/bin/perl

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

    ',Nerd=>'James Payne ');

    %HowItWas = @HowItIs{Dumb,Fat};

    print values(%HowItWas);

    print "\n\n";

    print keys(%HowItWas);

    The result?

      YoMama

      You

    We aren't limited to creating just other hashes with our hashes. We can also create variables:


    #!/usr/bin/perl

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

    ',Nerd=>'James Payne ');

    $HowItWas = @HowItIs{Dumb};

    print $HowItWas;


    Which gives us:

      You

    And we can also create lists as well:

    #!/usr/bin/perl


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

    ',Nerd=>'James Payne ');

    @HowItWas = @HowItIs{Dumb,Fat,Nerd};

    print @HowItWas;

    Here we are given:

      You YoMama James Payne

    Conclusion

    We covered a lot of ground in this article, however, there is much more to go. In our next tutorial we will learn to add records to hashes, remove them, check to see if a record already exists, create multidimensional lists, and much, much more. So be sure to stop by often.

    Till then...



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT