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

Re: Sort hash keys

by Eily (Vicar)
on Mar 18, 2014 at 17:25 UTC ( #1078826=note: print w/replies, xml ) Need Help??


in reply to Sort hash keys

cmp uses alphabetical order, you want numerical order on the number alone. One simple solution would be to have a function to extract the number out of the key and do : @sortedKeys = sort { $planets{$a} <=> $planets{$b} or getNum($a) <=> getNum($b) } keys %cuantas_veces_sale;

This not the most effective solution, but it works well enough. To get the number inside the string you can use a regex like /(\d+)/, tr/// or substr depending on what you find the easier. The last two are perhaps more user-friendly, but require that your keys have a consistant format ("hs" and then the number for exemple).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1078826]
help
Chatterbox?
[Corion]: I'm not fond of using a server hosted by other people, even if they claim to be GNU ;)
[Discipulus]: gee a social network with the family.. is this a dread? ;=)
[Corion]: Discipulus: Nah, I'm thinking of posting more short stuff, but not all of it is fit for blogs.perl.org, more something a bit longer than Twitter but shorter than my usual b.p.o module posts
[Corion]: But Google+ is tedious and I'd like to keep my posts on an open platform instead, which can be followed via RSS

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-05-26 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?