in reply to Hash sorting

Hashes won't preserve order. You can use Tie::IxHash, which sets up a different, order preserving, data structure with hash semantics.

You could keep a sorted array of keys from your hash, instead.

my @key_sorted = sort keys %hash; #or my @val_sorted = sort {$hash{$a} cmp $hash{$b}} keys %hash;

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Hash sorting
by Limbic~Region (Chancellor) on May 15, 2006 at 12:44 UTC
    Zaxo,
    Tie::IxHash is not designed for hash sorting which this question is about. It does allow you to preserve insertion order so that would require you to know in advance every single key/val pair that you want for the hash and enter them in that order. Ok, it does provide extremely rudimentary sorting capability (asciibetically by keys or vals), but that is more of an afterthought.

    If you want your hash to stay sorted using an arbitrary criteria regardless of what you do to it, use Tie::Hash::Sorted.

    Cheers - L~R