CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sorting an array of hashes

 



rajaryan
New User

Mar 25, 2010, 2:02 AM

Post #1 of 5 (399 views)
sorting an array of hashes Can't Post


Code
@aoh =( 
{
3 => 15,
4 => 8,
5 => 9,
},
{
3 => 11,
4 => 25,
5 => 6,
},
{
3 => 5,
4 => 18,
5 => 5,
},
{
0 => 16,
1 => 11,
2 => 7,
},
{
0 => 21,
1 => 13,
2 => 31,
},
{
0 => 11,
1 => 14,
2 => 31,
},
);

I want to the hashes in each array index sorted in reverse order based on values..

@sorted = sort { ........... please fill this..........} @aoh;

expected output

Code
@aoh =( 
{
4 => 8,
5 => 9,
3 => 15,
},
{
5 => 6,
3 => 11,
4 => 25,
},
{
5 => 5,
3 => 5,
4 => 18,
},
{
2 => 7,
1 => 11,
0 => 16,
},
{
1 => 13,
0 => 21,
2 => 31,
},
{
0 => 11,
1 => 14,
2 => 31,
},
);



7stud
Enthusiast

Mar 25, 2010, 2:56 AM

Post #2 of 5 (385 views)
Re: [rajaryan] sorting an array of hashes [In reply to] Can't Post

A statement such as this:


Code
@sorted = sort { ........... please fill this..........} @aoh;


says that you want to put an element of @aoh in some type of order relative to the other elements in the array. The elements of @aoh are hash references. Which hash reference should be at $aoh[0], and which hash reference should be $aoh[1], etc.? Your desired output shows that you don't care what order the hash references are in the array, so sorting the array is not what you want to do. What you care about is ordering the key/value pairs inside each hash, which would imply a for loop that steps through each element of the array, and then performs some operation on the current hash.

However, by definition hashes are unordered so you can't put hashes in any type of order.


(This post was edited by 7stud on Mar 25, 2010, 3:07 AM)


thillai_selvan
Novice

Mar 25, 2010, 3:09 AM

Post #3 of 5 (372 views)
Re: [rajaryan] sorting an array of hashes [In reply to] Can't Post

One simpler way is as follows

Code
use strict; 
use warnings;
my @aoh =(
{
3 => 15,
4 => 8,
5 => 9,
},
{
3 => 11,
4 => 25,
5 => 6,
},
{
3 => 5,
4 => 18,
5 => 5,
},
{
0 => 16,
1 => 11,
2 => 7,
},
{
0 => 21,
1 => 13,
2 => 31,
},
{
0 => 11,
1 => 14,
2 => 31,
},
);
for (my $i=0;$i<=$#aoh;$i++ )
{
my $val = $aoh[$i];
my $ref = \%$val;
#print "$aoh[$i]\n";
my @values = sort {$a <=> $b} values %{$ref};
print "Sorted values : @values\n";
}



thillai_selvan
Novice

Mar 25, 2010, 3:20 AM

Post #4 of 5 (371 views)
Re: [rajaryan] sorting an array of hashes [In reply to] Can't Post

And also you can use the following code.


Code
use strict; 
use warnings;
my @aoh =(
{
3 => 15,
4 => 8,
5 => 9,
},
{
3 => 11,
4 => 25,
5 => 6,
},
{
3 => 5,
4 => 18,
5 => 5,
},
{
0 => 16,
1 => 11,
2 => 7,
},
{
0 => 21,
1 => 13,
2 => 31,
},
{
0 => 11,
1 => 14,
2 => 31,
},
);
foreach my $href (@aoh)
{
my @sorted_keys = sort {$href->{$a} <=> $href->{$b} } keys %$href;
foreach my $key (@sorted_keys)
{
print "$key => $href->{$key}\n"
}
print "\n";
}



murugaperumal
Novice


Mar 25, 2010, 3:23 AM

Post #5 of 5 (369 views)
Re: [rajaryan] sorting an array of hashes [In reply to] Can't Post


Code
foreach(@aoh) 
{
my %a = %$_;
my %c= reverse %a;
foreach my $key (sort { $a <=> $b } keys%c)
{
print "$key=> $c{$key}\n";
}
print "\n";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives