Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > Perl

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Perl Hash : Tips & Tricks

By pradeep pradeep is offline

On 10th March, 2008
Cool Perl Hash : Tips & Tricks

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Perl Hash : Tips & Tricks link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

Introduction To Hashes In Perl (http://www.go4expert.com/forums/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: Perl
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: Perl
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: Perl
$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: Perl
$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}, ";
 }
Old 04-04-2008, 07:45 PM   #2
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Perl Hash : Tips & Tricks


shabbir is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Web Design Tricks and Tips daworm Web Design 12 10-20-2009 07:13 PM
Introduction To Hashes In Perl pradeep Perl 3 02-25-2008 02:39 PM
Windows XP Tricks & Tips pradeep Windows 0 05-23-2007 08:22 PM
SSH Tips and Tricks Febian Web Development 0 02-21-2007 09:44 PM
PERL resources coderzone Perl 3 08-02-2006 12:53 AM

 

All times are GMT +5.5. The time now is 05:10 AM.