Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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?
[marto]: oddly the last time I was there I saw a zeppelin in flight for the first time
[Corion]: ambrus: I don't know how far the budget estimate has been overrun yet
[Corion]: marto: Heh ;) No, you were likely at Tegel or Schönefeld, the new one ("BER") will be somewhat outside of Berlin ;)
[marto]: Corion Schoenfeld, but my friend has been telling me about this new airport for about 7-8 years now :P
[marto]: I doubt it even exists as a building site :P
[ambrus]: Corion: does building airports "somewhat outside of" a city ever work? cities always grow around the airport quickly, because it's practical to be close to it, and after that, people start to complain that the airplanes land so close to their house and sh
[ambrus]: ould keep down the noise

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2017-02-23 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (346 votes). Check out past polls.