i am trying to sort the hash based the third value in the array then print them out. for some reason it is not doing what i expected. thanks for the help. note if i change the sorting operator from cmp to == i get the following output:use strict; use Data::Dumper; my $market_hash; my $name = "a"; my $i = 0; my $sort_by = 111; for($i = 0;$i<15;$i++) { my @test; my $whatever = "sadfa"; push(@test, $whatever); push(@test, $whatever); push(@test, $sort_by); $market_hash->{$name} = \@test; $name .= "a"; $sort_by--; } print &Dumper($market_hash); foreach my $market (sort { $market_hash->{$a}[2] cmp $market_hash->{$ +b}[2] } keys (%{$market_hash}) ) { print "$market ---------------- $market_hash->{$market}[2]\n"; } #################### OUPUT ##################### C:\>test.pl aaaaaaaaaaaa ---------------- 100 aaaaaaaaaaa ---------------- 101 aaaaaaaaaa ---------------- 102 aaaaaaaaa ---------------- 103 aaaaaaaa ---------------- 104 aaaaaaa ---------------- 105 aaaaaa ---------------- 106 aaaaa ---------------- 107 aaaa ---------------- 108 aaa ---------------- 109 aa ---------------- 110 a ---------------- 111 aaaaaaaaaaaaaaa ---------------- 97 aaaaaaaaaaaaaa ---------------- 98 aaaaaaaaaaaaa ---------------- 99
thanksC:\>test.pl aaaaaaaaaaaaaaa ---------------- 97 aaaaaaaaaaa ---------------- 101 aaaaaaa ---------------- 105 aaaaaaaaa ---------------- 103 aaaaa ---------------- 107 a ---------------- 111 aaaaaaaaaaaaaa ---------------- 98 aaaaaaaaaaaa ---------------- 100 aaaaaaaaaa ---------------- 102 aaaaaaaaaaaaa ---------------- 99 aaaa ---------------- 108 aaa ---------------- 109 aa ---------------- 110 aaaaaa ---------------- 106 aaaaaaaa ---------------- 104
In reply to trouble sorting a hash of arrays by bear0053
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |