in reply to re-key a hash
Then clear your hash and assign the values using sequential keys:my @values = @hash{ sort { $a <=> $b } keys %hash };
or do it all in place without a temporary array:%hash = (); @hash{ 0..@values-1 } = @values;
Update:@hash{ keys(%hash), 0..keys(%hash)-1 } = ((undef) x keys(%hash), @hash +{ sort { $a <=> $b } keys %hash }); delete @hash{ grep !defined $hash{$_}, keys %hash };
almost works, but the keys(%hash) on the left is evaluated after the hash has been empty :(@hash{ 0..keys(%hash)-1 } = delete @hash{ sort { $a <=> $b } keys %has +h };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: re-key a hash
by Aristotle (Chancellor) on Aug 02, 2004 at 22:21 UTC | |
by ysth (Canon) on Aug 02, 2004 at 22:27 UTC |