i have the following code:
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
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:
C:\>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
thanks

UPDATE:
<=> WORKED! Thanks

In reply to trouble sorting a hash of arrays by bear0053

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.