Perl
  Home arrow Perl arrow Page 2 - Hash Mania With Perl
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? 
Google.com  
PERL

Hash Mania With Perl
By: D. Jasmine Merced
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 42
    2004-04-15


    Table of Contents:
  • Hash Mania With Perl
  • Assigning Key/Value Pairs
  • Sorting Hashes
  • Subbing Out Sorting

  • 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


    Hash Mania With Perl - Assigning Key/Value Pairs
    ( Page 2 of 4 )

    Hashes consist of one or more individual keys and its associated value. Each key and value are called pairs. There are several ways to insert these pairs into hashes, outline below.

    If you know (at least some of) the key/value pairs that you would like to use, the following is the most straightforward way to assign pairs to hashes:

     %hash = ( 
    apples 
    => 6
    oranges 
    => 5
    pears 
    => 3
    grapes 
    => 2
    ); 



    The above is a more readable way to assign key/value pairs. Let's not forget the importance of having easy to read code. A less readable way to assign keys and values to hashes is below:

     %hash qw(apples 6 oranges 5 pears 3 grapes 2); 



    Perl will automatically convert the above to key/value pairs as if you used the arrows => in the first example. We recommend the first format's example for readability, though the formats can be used interchangeably.

    You can also add each key/value pair individually. The following line adds a new key/value pair to our original hash.

     $hash{peach} = 3



    If the original hash did not exist, this line would have created a new hash and inserted the first key/value pair as defined. The process by which a variable can spring into life like this is called autovivification.

    This is useful if you need to loop through a data file and would like to insert data from the file to a hash.

     open FILE"fruits.txt" or die $!; 
    while 
    (){ 
    chomp

    my 
    @line split(/    /); 
    $hash
    {$line[0]} = $line[1]; 

    close FILE 
    or die $!; 



    Removing Pairs from Hashes
    Now that we know how to add pairs to hashes, we need to know how to get rid of them. Deleting a pair is as easy as knowing the key of the pair you want deleted:

     delete $hash{peach}; 



    Now, the pair whose key is peach is gone. But what if you wanted to delete the entire hash? You can either loop through the entire hash and delete each key (inefficient) or you can undef it:

      undef %hash



    Do not use:

     %hash undef



    This will not obliterate the hash, it will assign a single key/value pair of undef/undef. If you want to remove all keys from the hash, but still keep %hash as an "active" variable, use:

     %hash = (); 



    Looking inside Hashes
    We now know how to add and remove pairs from hashes, but how to see what pairs are there? As with nearly everything Perl, TIMTOWTDI (there is more than one way to do it). Here, we'll look at a few examples on how to loop inside hashes and take a peek at what's inside. These examples assume you're already familiar with loops.

    Using foreach

     foreach my $key (keys %hash) { 
    print 
    "$key = $hash{$key}
    "




    The my $key localizes the scalar to this loop (and prevents the "uninitialized variable" errors when running under warnings).

    Using map

     print map "$_ = $hash{$_}
    "
    keys %hash




    Using while/each

     while (($key,$value) = each %hash){ 
    print 
    "$key = $value
    "




     
     
    >>> More Perl Articles          >>> More By D. Jasmine Merced
     

       

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek