in reply to Re: Permanently sort a hash
in thread Permanently sort a hash

Thank you for such a quick reply!
The reason why I wanted it sorted, is because I have to print frequently a hash where key order matters. Instead of sorting every time the keys before looping I wanted to have it sorted permanently.

Replies are listed 'Best First'.
Re^3: Permanently sort a hash
by kennethk (Abbot) on Jun 04, 2009 at 15:14 UTC
    ikegami's solution is a good one then - cache the sorted keys. You can even forgo the foreach loop using hash Slices:

    my @sorted_keys = sort { $a <=> $b } keys(%foo); print join("\n",@hash{@sorted_keys})