in reply to Consistent order of (keys %hash)?

The answer is yes. However, this is not consistent between different hashes, so be careful. In this message, gbarr demonstrates that assign order can change the order of keys.

Replies are listed 'Best First'.
Re: Re: Consistent order of (keys %hash)?
by clintp (Curate) on Nov 09, 2001 at 01:30 UTC
    Agreed.

    To the original poster: relying on an implementation detail like this to do something useful is a bad idea. Especially when it's clearly stated (or at least implied) in the language documentation that the order shouldn't be relied on.

    It might be fine for a JAPH or a 1-liner, but using this feature anywhere important is foolish; if the implementation changes, or someone ties the hash an application could screw up...

      The fact that repeated calls to keys %hash always returns the exact same thing right down to the order of the keys isn't an implementation detail; it is documented behavior. If you don't modify %hash, then you can be assured that the order of items returned by keys, values, and each is consistant no matter how many times you call them.

      A tied hash that violates this rule would be rather strange. Even DB* hashes obey this rule.

      It is an important rule because it allows code like:     %copy{keys %hash}= values %hash; to work (which efficiently merges %hash into %copy).

              - tye (but my friends call me "Tye")