in reply to re-key a hash

First get a list of the values in sorted key order. Note that @hash{ LIST } produces a list of the values corresponding to the keys in list.
my @values = @hash{ sort { $a <=> $b } keys %hash };
Then clear your hash and assign the values using sequential keys:
%hash = (); @hash{ 0..@values-1 } = @values;
or do it all in place without a temporary array:
@hash{ keys(%hash), 0..keys(%hash)-1 } = ((undef) x keys(%hash), @hash +{ sort { $a <=> $b } keys %hash }); delete @hash{ grep !defined $hash{$_}, keys %hash };
Update:
@hash{ 0..keys(%hash)-1 } = delete @hash{ sort { $a <=> $b } keys %has +h };
almost works, but the keys(%hash) on the left is evaluated after the hash has been empty :(

Replies are listed 'Best First'.
Re^2: re-key a hash
by Aristotle (Chancellor) on Aug 02, 2004 at 22:21 UTC

    The OP is not interested in preserving the old keys, so a bunch of the hoops you're jumping through is superfluous.

    Makeshifts last the longest.

      The hoops are necessary to avoid preserving the old keys.