Gorio3721 has asked for the wisdom of the Perl Monks concerning the following question:
I'm sure this is a simple question but how do I sort the keys of a hash in numeric order if the keys are of the form Drive0, Drive1 ... Drive10, Drive11, etc.
I have tried the following:
sub mysort { $a <=> $b; } foreach $i ( sort mysort ( keys %myhash ) ) { print "$i\n"; }
This only complains that Drive1 is not numeric.
If I use the standard ASCII sorting then it gets sorted Drive1, Drive10, Drive11, Drive2, etc.
Thanks in advance for the help.
Gorio
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "numeric" sort of keys?
by Zaxo (Archbishop) on Sep 25, 2003 at 17:59 UTC | |
by Gorio3721 (Acolyte) on Sep 25, 2003 at 20:54 UTC | |
by Limbic~Region (Chancellor) on Sep 25, 2003 at 21:24 UTC | |
by Gorio3721 (Acolyte) on Sep 25, 2003 at 20:58 UTC | |
by Mr. Muskrat (Canon) on Sep 25, 2003 at 21:04 UTC | |
|
Re: "numeric" sort of keys?
by davido (Cardinal) on Sep 25, 2003 at 18:08 UTC | |
|
Re: "numeric" sort of keys? (natural)
by tye (Sage) on Sep 26, 2003 at 04:15 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |