in reply to Re: What makes an array sorted and a hash unsorted?
in thread What makes an array sorted and a hash unsorted?
The difference is in this code:
print $_ foreach @array; print $_ foreach keys %hash
So if keys sorted the keys before returning them, the hash would be considered sorted? nah.
Another way to look at is that an array keeps it's sort order, where a hash doesn't.
keys may not return the keys in order, but it could. "f" is not suddenly less than "b" because keys decides to return foo before bar.
Again, it can't be that hashes aren't ordered simply because Perl doesn't provide a builtin function to return them sorted like it does for arrays.
The hash's list will change with every new run.
Nit:
s/will/may/
Perl can and will return them in the same order in some circumstances, even after the 5.8.1 change to add randomisation.
$ perl -e'system(perl => "-le %h = map { \$_=>1 } qw(a b c d); print k +eys %h") for 1..10' cabd cabd cabd cabd cabd cabd cabd cabd cabd cabd
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: What makes an array sorted and a hash unsorted?
by DStaal (Chaplain) on Jun 01, 2009 at 17:15 UTC | |
by ikegami (Patriarch) on Jun 01, 2009 at 17:25 UTC | |
|
Re^3: What makes an array sorted and a hash unsorted?
by shmem (Chancellor) on Jun 01, 2009 at 17:36 UTC | |
by ikegami (Patriarch) on Jun 01, 2009 at 17:44 UTC | |
|
Re^3: What makes an array sorted and a hash unsorted?
by John M. Dlugosz (Monsignor) on Jun 01, 2009 at 19:49 UTC | |
by ikegami (Patriarch) on Jun 01, 2009 at 20:09 UTC |