in reply to Sort hash keys
The easiest answer here would probably be to keep leading 0's on your keys, e.g. hs01 instead of hs1. Barring that, you could do the parsing in a named sub:
or possibly cache the parse in a hash ahead of time:use strict; my %planets; my %cuantas_veces_sale; for my $nombre (sort keysort keys %cuantas_veces_sale) { print "$nombre\n"; }; sub keysort { return $planets{$a} <=> $planets{$b} if $planets{$a} <=> $planets{ +$b}; my ($num_a) = $a =~ /(\d+)/; my ($num_b) = $b =~ /(\d+)/; $num_a <=> $num_b; }
use strict; my %planets; my %cuantas_veces_sale; my %alphas = map {$_ => /(\D+)/} keys %cuantas_veces_sale; my %numerics = map {$_ => /(\d+)/} keys %cuantas_veces_sale; for my $nombre (sort { $planets{$a} <=> $planets{$b} or $alphas{$a} cmp $alphas{$b} or $numerics{$a} cmp $numerics{$b} } keys %cuant +as_veces_sale) { print "$nombre\n"; };
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort hash keys
by cristianro87 (Initiate) on Mar 18, 2014 at 17:30 UTC |