in reply to Re: re-key a hash
in thread re-key a hash

Much better :)

{ local $_ = keys( %h ) -1; @h{ 0 .. $_ } = delete @h{ sort { $a<=>$b } keys %h }; }

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

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

    If we're using multiple statements anyway (not doing so was the point of my obscure ditty), I'd shuffle things around.

    { my @key = sort { $a <=> $b } keys %h; @h{ 0 .. $#key } = delete @h{ @key }; }

    That seems like a cleaner separation of concerns. The symmetry of the second line here seems more pleasing and makes it more robust — it remains valid regardless of how the list of keys is generated.

    Makeshifts last the longest.