Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting an array

Status
Not open for further replies.

mns9939

Programmer
Nov 21, 2001
19
US
Hi,

I have a hash array with key-value pair like
1-AAAA
2-BBBB
.
.
.
11-KKKKK
....
..
21-LLLL

however, when i loop through the array, it does not print the values in the order it was put in.
How do i make it to print the values in the order in was put in?

Thanks for your help,
Madhu
 
The easiest way to print the keys or values in order is to sort them like:
Code:
@sortedkeys = sort(keys %hash);
@sortedvalues = sort(values %hash);
So if you want to print the key-value pair sorted on keys.
Code:
foreach $key (@sortedkeys) {
    print "key=$key value=$hash{$key} \n";
}
 
this is a numerical sort
@sorted = sort { ( $a ) <=> ( $b ) } @unsorted;


this is a alphabetical sort
@sorted = sort { lc( $a ) cmp lc( $b ) } @unsorted;


hope this helps ya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top