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
In reply to Re^2: What makes an array sorted and a hash unsorted?
by ikegami
in thread What makes an array sorted and a hash unsorted?
by ikegami
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |