how to efficiently sort hash without using keys()
- From: konfera@xxxxxx (Ing. Branislav Gerzo)
- Date: Wed, 25 May 2005 11:45:17 +0200
Hi beginners@xxxxxxxx,
anyone has better idea how to sort hash by key without using keys()
function? (I can't use it, it resets iterator...).
my bruteforce looks:
use strict;
use warnings;
my %hash = ( 1 => 'one',
2 => 'two',
3 => 'three'
);
my @array = ();
while (my ($key, $value) = each(%hash)) {
push @array, $key;
}
@array = sort @array;
but I think there should be better way. Anyone?
I mean, snippet should do something like:
for my $key (sort {$a <=> $b} keys %hash) {
#do something $key
}
thanks.
--
--. ,-- ,- ICQ: 7552083 \|||/ `//EB: www.2ge.us
,--' | - |-- IRC: [2ge] (. .) ,\\SN: 2ge!2ge_us
`====+==+=+===~ ~=============-o00-(_)-00o-================~
Hard disk parking: .10/mb for the 1st 2 hrs; all day=$25
.
- Follow-Ups:
- Re: how to efficiently sort hash without using keys()
- From: Paul Johnson
- Re: how to efficiently sort hash without using keys()
- Prev by Date: RE: Can't use subscript in angle brackets
- Next by Date: RE: how to efficiently sort hash without using keys()
- Previous by thread: output in a single line
- Next by thread: Re: how to efficiently sort hash without using keys()
- Index(es):