in reply to re-key a hash

$hash{-10} = 'bar'; $hash{-1} = 'foo'; $hash{12} = 'baz'; %hash = map { (keys %hash) - 1 => delete $hash{$_} } sort {$b <=> $a} keys %hash; print map {"$_ => $hash{$_}\n"} sort { $a <=> $b } keys %hash;

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

    What's the point of your delete? You're assigning a completely new list to that hash in the end anyway.

    It certainly doesn't seem like the "best" version to me, at least in terms of my usual metric: self-documentation.

    Makeshifts last the longest.

      Since ccn didn't answer your question... The delete is there to make the calculation of the new keys ( (keys %hash) - 1 ) work.

      - tye        

      Ok, it's not for production use, but it's fun. I like such stuff.