Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: how do i sort hash by key numerically

by Jouke (Curate)
on Mar 07, 2001 at 13:50 UTC ( [id://62699]=note: print w/replies, xml ) Need Help??


in reply to how do i sort hash by key numerically

This would do the trick:
#!/usr/bin/perl -w %hash=( 89=>3, 45=>2, 1 =>5, 40=>3); foreach (sort { $a <=> $b } keys(%hash) ) { print "key: $_ value: $hash{$_}\n" }
Jouke Visser, Perl 'Adept'

Replies are listed 'Best First'.
Re: Answer: how do i sort hash by key numerically
by Tyke (Pilgrim) on Mar 07, 2001 at 16:35 UTC
    Are you sure that this doesn't sort typographically rather than numerically. I think the {$a<=>$b} block cited by arhuman is necessary here.
    -- I knock my pate and fancy wit will come Knock as I please, there's no one at home a pontiff paraphrased

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found