Perl Hash : Tips & Tricks

Discussion in 'Perl' started by pradeep, Mar 10, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Introduction To Hashes In Perl (http://www.go4expert.com/showthread.php?t=8518)

    Preserve Add Order



    Perl doesn't guarantee the order of the elements of hash, they may be in a different order everytime you access the hash. It's a much wanted, rather expected behaviour that I get my elements in the order I added them to the hash. The Tie::IxHash module helps us out of this problem, all we need to do it to tie the has to this module, after which you'll find the hash elements in the same order you has added them.

    Here's a simple demonstration.

    Code:
     use Tie::IxHash;
     
     tie (%my_hash, Tie::IxHash);
     $my_hash{'PHP'} = 'Cool';
     $my_hash{'ASP'} = 'Old';
     $my_hash{'Perl'} = 'Amazing';
     $my_hash{'Ruby'} = 'On Rails';
     
     while my $k (keys %my_hash) 
     {
         print "$k=>$my_hash{$k}, ";
     }
     

    Use 2 Arrays For Keys & Values Of A Hash



    Code:
     my @keys = qw/PHP ASP Perl Ruby/;
     my @values = ('Cool','Old','Amazing','On Rails');
     
     my %my_hash;
     
     @my_hash{@keys} = @values;
     
     while my $k (keys %my_hash) 
     {
         print "$k=>$my_hash{$k}, ";
     }
     
    Some might be confused with the "@..{..}" notation, it's the notation for accessing a hash slice, by which you can access a range/collection of hash keys at once.

    A very useful implementation of hash slice is while fetching rows using DBI module. Use hash slices we can get a hash of the record row, much like the PHP equivalent mysql_fetch_row, which can be used to fetch a associative array.

    Sorting Hashes



    By keys:

    Unlike an array you cannot sort a hash, cause Perl uses it's own algorithm the order of hash elements which is not at all predictable. So, the trick is to get the keys of a hash and sorted them insted to get the desired results.

    Code:
     $my_hash{'PHP'} = 'Cool';
     $my_hash{'ASP'} = 'Old';
     $my_hash{'Perl'} = 'Amazing';
     $my_hash{'Ruby'} = 'On Rails';
     
     while my $k (sort keys %my_hash) 
     {
         print "$k=>$my_hash{$k}, ";
     }
     
    By values:

    Like the above example, here we will again be using the keys but differently. We use the string comparison operator "cmp" and <=> for interger comparison.

    Code:
     $my_hash{'PHP'} = 'Cool';
     $my_hash{'ASP'} = 'Old';
     $my_hash{'Perl'} = 'Amazing';
     $my_hash{'Ruby'} = 'On Rails';
     
     while my $k (sort {$my_hash{$a} cmp $my_hash{$b}} keys %my_hash) 
     {
         print "$k=>$my_hash{$k}, ";
     }
     
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice