Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: how to sort hash of array by value

by FunkyMonk (Chancellor)
on Oct 01, 2007 at 22:06 UTC ( [id://642011]=note: print w/replies, xml ) Need Help??


in reply to how to sort hash of array by value

You forgot to sort it!
use Data::Dumper; my %colorhash = ( CCgray => [ "0","0","0" ], CCwhite => [ "1","0","0" ], CCgrey => [ "0","2","0" ], CCBlue => [ "0","0","3", ] ); my @light2dark = sort numsort (keys %colorhash); print Dumper \@light2dark; sub numsort { $colorhash{$a}[0] <=> $colorhash{$b}[0] or $colorhash{$a}[1] <=> $colorhash{$b}[1] or $colorhash{$a}[2] <=> $colorhash{$b}[2]; }

Output:

$VAR1 = [ 'CCgray', 'CCBlue', 'CCgrey', 'CCwhite' ];

Replies are listed 'Best First'.
Re^2: how to sort hash of array by value
by blkperl (Initiate) on Oct 01, 2007 at 22:51 UTC
    oops, but it still does not change the order based on the value. There is not change in the hash order.
      The hash order is always random, and has nothing to do with the order in which that hash was created.

      Compare the following snippet with the original:

      use Data::Dumper; my %colorhash = ( CCgray => [ "0","0","0" ], CCwhite => [ "1","0","0" ], CCgrey => [ "0","2","0" ], CCBlue => [ "0","0","3", ] ); my @light2dark = keys %colorhash; print Dumper \@light2dark;

      PS Have you enclosed your original program within <code>...</code> tags yet?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://642011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found