in reply to problem while sorting hash

You aren't creating a sorted hash. There is no such thing as a sorted hash (well, unless you use something like Tie::IxHash).

Hashes are by nature unordered. All you are doing here is adding keys to a hash. You're adding them in the correct order, but they are stored in a (seemingly) random order.

If you want things stored in a fixed order, then use an array. Not a hash.

--

See the Copyright notice on my home node.

"The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: problem while sorting hash
by Limbic~Region (Chancellor) on Mar 20, 2007 at 22:11 UTC
    davorg,
    There is no such thing as a sorted hash (well, unless you use something like Tie::IxHash).

    Minor nit: Tie::IxHash provides a facility to maintain insertion order. Any relationship of this order to a sorted list must be done by the user.* On the other hand, Tie::Hash::Sorted does provide a very flexible and relatively efficient way to keep your hash sorted regardless of hash modifications.

    I am not advocating using tied hashes without proper consideration but rather trying to clarify a common misconception about Tie::IxHash that others seem to have.

    Cheers - L~R

    It does provide methods to get the keys or values, normally returned by insertion order, in asciibetical order. This does not re-order the hash.
A reply falls below the community's threshold of quality. You may see it by logging in.