in reply to Permanently sort a hash

If you don't want to call sort repeatedly, save the result.

my @sorted_keys = sort { $a <=> $b } keys(%foo);

Update: Or if you no longer need the keys:

my @sorted_values = @foo{ sort { $a <=> $b } keys(%foo) };

Replies are listed 'Best First'.
Re^2: Permanently sort a hash
by Limbic~Region (Chancellor) on Jun 04, 2009 at 15:16 UTC
    ikegami,
    While you know and it should be obvious, it may be worth pointing out that any modification to the keys of the hash (inserts or deletes) requires the sorted keys to be re-cached.

    Cheers - L~R

      Yes, but that would also be the case for the ideal solution the OP posted.