Home arrow Perl Programming arrow Page 3 - Hash Mania With Perl

Sorting Hashes&toc - Perl

Perl hashes are extremely useful data structures that allow us to associate one piece of data to another. In this article, Jasmine will review hashes and introduce some of their more advanced uses.

TABLE OF CONTENTS:
  1. Hash Mania With Perl
  2. Assigning Key/Value Pairs
  3. Sorting Hashes
  4. Subbing Out Sorting
By: D. Jasmine Merced
Rating: starstarstarstarstar / 44
April 15, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
If you've actually tested the samples above, you'll have noticed that the hashes printed out in seemingly random order. This is because hashes are stored based on memory location, not alphabetically or numerically. But have heart, it's easy to sort hashes.

There are 3 ways to sort in Perl: ASCIIbetically, numerically or alphabetically.

Every character (number, letter or metacharacters) has an ASCII code associated with it. Letters have separate ASCII codes for each of the cases (upper and lower case). For example, the letter A is 065 and the letter a is 097. So A is "less than" a (065 < 097). with this in mind, let's create a simple hash that uses both upper and lower cases in its keys:

  %hash = ( 
Apples 
=> 1
apples 
=> 4
artichokes 
=> 3
Beets 
=> 9
); 

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




The above code will print:

Apples = 1
Beets = 9
apples = 4
artichokes = 3

Because the letter B is 066 in ASCII code, it is "less than" 097, the letter for A. This yields some strange results, but you may wish to use it one day :)

"To sort strings without regard to case, run $a and $b through lc before comparing:" using the cmp comparison operator. This tells Perl to sort letters and ignore case.

 foreach my $key (sort {lc($acmp lc($b)} keys %hash) { 
print 
"$key = $hash{$key}
"




This correctly prints:

Apples = 1
apples = 4
artichokes = 3
Beets = 9

Hash Slices
"A slice is a section or number of elements from a list, array or hash." Essentially, you can add or delete key/value pairs en masse using slices, which are named using the @ at symbol. To give an example of slices, consider the following:

 @months qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); 

@monthnums{@months}= 1..$#months+1; 



Here, we've just created a hash named %monthsnum using a hash slice. It added each of the elements of the @months array as keys, and the values are 1 through 12 to each month. Because @months are in order, adding 1 through 12 assigns the correct month number value to each key.

So you've done the hash slice and now want to print out the results to make sure it's correct.

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

}; 



Prints:

Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
Jun = 6
Jul = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12

We've already seen how to sort hashes in Sorting Hashes above, and we've just added to it.

 sort {$nums{$a} <=> $nums{$b}} 



sorts the hash numerically based on the values of the hash instead of the keys. This way, our months appear in the correct year order.



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

blog comments powered by Disqus
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: