Re: Hash key ordering?
by jmcnamara (Monsignor) on May 02, 2002 at 11:38 UTC
|
Hashes with the same keys won't necessarily return the keys in the same order, as shown below. However, as Juerd points out, the order shouldn't change for a given hash if the keys don't change, also shown below.
The only problem that I can see is that the order the keys are returned in might change in a future release. So, I wouldn't rely on that.
#!/usr/bin/perl -wl
use strict;
my %h1;
my %h2;
@h1{1..10} = undef;
print join " ", keys %h1;
@h1{5..10, 1..4} = undef;
print join " ", keys %h1;
@h2{5..10, 1..4} = undef;
print join " ", keys %h2;
__END__
Prints:
1 2 3 10 4 5 6 7 8 9
1 2 3 10 4 5 6 7 8 9
1 2 3 4 10 5 6 7 8 9
--
John.
| [reply] [d/l] |
|
| [reply] |
|
Thank you all for all the help on that issue.
| [reply] |
Re: Hash key ordering?
by tadman (Prior) on May 02, 2002 at 10:37 UTC
|
The safe answer is "No", the order of things in the hash is arbitrary. In practice, you might find some relationship between data and order. This, however, may change from version to version, or platform to platform, depending on how the hash is implemented. In some cases, you might not be using a hash at all, but something tied to another source. Who knows what might be going on in there.
If you care about order, always ask for it sorted. Don't bother sorting, though, if you are merely iterating, since that usually worthless. | [reply] |
Re: Hash key ordering?
by choocroot (Friar) on May 02, 2002 at 11:00 UTC
|
| [reply] |
Re: Hash key ordering?
by Juerd (Abbot) on May 02, 2002 at 10:39 UTC
|
If the hash keys do not change, their order will not change.
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
| [reply] |
Re: Hash key ordering?
by Elian (Parson) on May 02, 2002 at 13:37 UTC
|
You can count on it within runs of a particular program. The order is dependent on the hashing function, which can't change during the course of a program run. (Well, it can, but it takes a huge amount of work) The hashing function has changed at least once during perl 5's life though, and may change again, so different versions of perl may give you different ordering.
All bets are off if the hash is tied, of course. | [reply] |
Re: Hash key ordering?
by rinceWind (Monsignor) on May 02, 2002 at 12:04 UTC
|
| [reply] |
Re: Hash key ordering?
by Cybercosis (Monk) on May 02, 2002 at 17:49 UTC
|
| [reply] |
|
Most people's replies seem to be focusing on the details of the implementation, but this isn't the greatest idea. One can, however, make certain conclusions based only on guarantees in the documentation:
(perldoc -f keys)
The keys are returned in an appar-
ently random order. The actual random order is
subject to change in future versions of perl, but
it is guaranteed to be the same order as either
the `values' or `each' function produces (given
that the hash has not been modified).
That's exactly how much you can count on - the fact that it is "the same order as" values() and each() means that it also must be the same order as future calls to keys() (by the transitivity property of the word "same"). If you make *any* changes to the hash, this guarantee goes away.
Of course, this is *not* the same as the OP's original phrase "provided of course that the keys are the same" - you can insert & delete things in a hash and end up with the same eventual set of keys, but your modifications may still change the order of keys().
| [reply] [d/l] |