Perl
  Home arrow Perl arrow Page 4 - 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 - Subbing Out Sorting
    ( Page 4 of 4 )

    Think it will introduce too many typos to remember the construct for alphabetical sorting? Then "sub" it! Let's say you use sorting very frequently in your programming and find it cumbersome to type in the above operations each time.

    Let's handle this by example:

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




    You can easily see that the lc($a) cmp lc($b) has been replaced by a subroutine call. Now, let's consider the following:

     sub ascend_num {$a <=> $b
    sub descend_num 
    {$b <=> $a
    sub ascend_alpha 
    {lc($acmp lc($b)} 
    sub descend_alpha 
    {lc($bcmp lc($a)} 
    sub ascend_alphanum 
    {$a <=> $b || lc($acmp lc($b)} 
    sub descend_alphanum 
    {$b <=> $a || lc($bcmp lc($a)} 



    The ascend_alphanum and descend_alphanum routines sort both alphabetically and numerically, so if you added "5 Spice Seasoning", "1 Star Flour", and "911 Hot Sauce" to %hash, it will sort the numbers numerically in addition to letters alphabetically.

    Apples = 1
    apples = 4
    artichokes = 3
    Beets = 9
    canadian = 9
    5 Spice Seasoning = 1
    10 Star Flour = 1
    911 Hot Sauce = 1

    Working with Hash References
    Have a hash reference and don't want to duplicate the subroutines to deal with them? It's easy... just pass the dereferenced keys to the sort routines:

     $hashref = %hash

    foreach my $key 
    (sort ascend_alpha keys %{$hashref}){ 
    print 
    "$key = $hashref->{$key}
    "




    Notice the hash deference %{$hashref} and the arrow dereferencer for the value $hashref->{$key}.

    Sorting by Hash Values
    What if you wanted to sort the values instead of the keys? Consider the following:

     foreach my $key (sort {$hash{$a} <=> $hash{$b}} keys %hash){ 
    print 
    "$key = $hash{$key}
    "




    This will print out:

    Apples = 1
    5 Spice Seasoning = 1
    10 Star Flour = 1
    APples = 2
    artichokes = 3
    apples = 4
    911 Hot Sauce = 4
    canadian = 9
    Beets = 9

    But let's say you also had text values -- let's change $hash{'Beets'} to "cans: 4 - 8.oz." and $hash{'Apples'} to "Delicious Red - 4 medium sized". You can have the hash sorted numerically and alphabetically by using the following:

     foreach my $key (sort {$hash{$a} <=> $hash{$b} || $hash{$acmp $hash{$b}} keys %hash){ 
    print 
    "$key = $hash{$key}
    "




    This will correctly print out:

    Beets = cans: 4 - 8.oz.
    Apples = Delicious Red - 4 medium sized
    5 Spice Seasoning = 1
    10 Star Flour = 1
    APples = 2
    artichokes = 3
    apples = 4
    911 Hot Sauce = 4
    canadian = 9

    Multidimensional Hashes
    Using key/value pairs is great, but what if you wanted to associate more than one value to a key? Using a slightly different construct, you can essentially use an array as a key's value:

     %hash = ( 
    Apples 
    => [4"Delicious red""medium"], 
    "Canadian Bacon" => [1"package""1/2 pound"], 
    artichokes 
    => [3"cans""8.oz."], 
    Beets 
    => [4"cans""8.oz."], 
    "5 Spice Seasoning" => [1"bottle""3 oz."], 
    "10 Star Flour" => [1"package""16 oz."], 
    "911 Hot Sauce" => [1"bottle""8 oz."], 
    ); 



    Now, to extract the values, you can treat them as an array of the hash:

     print $hash{"Canadian Bacon"}[1]; 



    ...will print package, because package is the second element of Canadian Bacon's "array". You can also add an predefined array to a hash value:

     @garlicstuff = (4"cloves""medium"); 
    $hash
    {"Garlic"} = [@garlicstuff]; 
    print $hash
    {"Garlic"}[1]; # prints cloves 



    But what if @garlicstuff had more elements than others? Let's say that @garlicstuff is

     @garlicstuff = (4"cloves""medium""chopped"); 



    instead? How do we print out all values for a key if one key can have 3 values, and another has 4 (or more) values?

     foreach my $key (sort ascend_alpha keys %hash){ 
    print 
    "$key: 
    "

    foreach my $val 
    (@{$hash{$key}}){ 
    print 
    "    $val
    "


    print 
    "
    "




    Because a multidimensional array's values are essentially arrays, a key's group of values can be dereferenced by using @{$hash{$key}}. The above code prints:

    10 Star Flour:
    1
    package
    16 oz.

    5 Spice Seasoning:
    1
    bottle
    3 oz.

    911 Hot Sauce:
    1
    bottle
    8 oz.

    Apples:
    4
    Delicious red
    medium

    artichokes:
    3
    cans
    8.oz.

    Beets:
    4
    cans
    8.oz.

    Canadian Bacon:
    1
    package
    1/2 pound

    Garlic:
    4
    cloves
    medium
    chopped

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