http://www.perlmonks.org?node_id=768842


in reply to Re^4: What makes an array sorted and a hash unsorted?
in thread What makes an array sorted and a hash unsorted?

It's not as easy as that (I'm playing Ikegamis adovate now ;-). One could make the argument that a hash %h=(0,'a',1,'b',2,'c'); and an array @a=('a','b','c'); are both ordered in the same way. No sequence of equivalent insertions and deletions will change that both will look the same when you print them with

print $h{$_},$a[$_] for (0..@a-1);

In older languages that would have been the only way to access arrays and print them.

That orderedness of the hash only breaks when you use print @a for which there is no direct substitute on the hash side (but could be built in a way that it comes out ordered). If you access them with a foreach (keys ...) loop, then you might as well use a foreach (sort keys ...) loop.

Of course you get a random list when you call keys(). But what if perl had a built-in function arraykeys() which gave back the numbers of the array in a random ordering. Would arrays then be both ordered and unordered or partially ordered or not ordered at all?. What if you had no keys() and each() functions in perl? Would a hash then be ordered or undefined in its orderedness?

As I said before, I am of the opinion that perl hashes are unordered and arrays are ordered, but proving it is not as simple as one might think.