in reply to re-key a hash
A hash is great if the data is sparsely populated, but if you make the keys sequential integers, why not go with an array?
my @array = @hash{ sort {$a <=> $b} keys %hash};Incidentally, note that you're deleting hash elements while iterating over it. You're not supposed to do that.
Update: To keep a hash, try something like this:
my %hash2; @hash2{ 0 .. (keys %hash)-1 } = map { $hash{$_} } sort {$a <=> $b} key +s %hash;
It's really ugly, though :)
Update 2 ysth and Aristotle both caught me sleeping. Pay no attention to the man in the classical poet outfit.
Cheers,
Ovid
New address of my CGI Course.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: re-key a hash
by Aristotle (Chancellor) on Aug 02, 2004 at 21:46 UTC | |
|
Re^2: re-key a hash
by ysth (Canon) on Aug 02, 2004 at 21:41 UTC | |
by Roy Johnson (Monsignor) on Aug 03, 2004 at 01:27 UTC | |
by ysth (Canon) on Aug 03, 2004 at 01:32 UTC |