Dear Masters,
Given the HoHoA below, and list of selected stores "@st", I want to sort the selected stores based on its earning (first element of the AoA)
my %bighash = ( 'set1' => { 'STORE1' => [10,['1','BOOK'],['2','PENCIL']], 'STORE2' => [8,['0','CHALK'],['4','PEN']], }, 'set2' => { 'STORE3' => [13,['1','BOOK'],['2','PENCIL']], 'STORE4' => [11,['0','CHALK'],['4','PEN']], }, 'set3' => { 'STORE5' => [2, ['1','BOOK'],['2','PENCIL']], 'STORE6' => [70,['0','CHALK'],['4','PEN']], }, 'set4' => { 'STORE7' => [19,['1','BOOK'],['2','PENCIL']], 'STORE8' => [20,['0','CHALK'],['4','PEN']], } ); # list of selected stores (st) my @st = ('STORE2','STORE3','STORE5'); foreach ( @st ) { foreach my $grp ( sort {$bighash{$b}{$_}[0] <=> $bighash{$a}{$_}[0]} keys %bighash ) { if ( defined $bighash{$grp}{$_}) { print "STORE: $_ EARNING:$bighash{$grp}{$_}[0]\n"; foreach my $j ( 1 .. (@{$bighash{$grp}{$_}})-1 ) { print join(",",@{$bighash{$grp}{$_}[$j]}),"\n"; } print "\n"; } } }
But, why my code above fail to produce the sorted selected stores like this?
STORE: STORE3 EARNING:13 1,BOOK 2,PENCIL STORE: STORE2 EARNING:8 0,CHALK 4,PEN STORE: STORE5 EARNING:2 1,BOOK 2,PENCIL
What's wrong with my "sorting syntax" above?

---
neversaint and everlastingly indebted.......

In reply to Sorting by Hash Slice by neversaint

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.