Perl-Sensitive Sunglasses | |
PerlMonks |
Re: What makes an array sorted and a hash unsorted?by jethro (Monsignor) |
on Jun 04, 2009 at 12:14 UTC ( [id://768415]=note: print w/replies, xml ) | Need Help?? |
That means that hashes are just as sorted as arrays in theory In theory (i.e. computer science theory) there are no arrays in perl
(ok, we could define UNDEF as a defined/actual/existing value of an array, but then why does scalar(@a) report 5 when there is a value in $a[37865]? ) In theory perls closest thing to arrays is an array, since for example $value==pop(push @a,$value)) (a fundamental property of theoretical arrays) while pop and push for hashes isn't even defined (even though someone could define it somehow in perl5.11 just to make a point ;-) In practice perls data structures are as shifty as the language itself and you can implement hashes in arrays as well as arrays in hashes. So the sentence "arrays are ordered and hashes are not" (I hope we all concur by now that 'sorted' was the wrong word) is merely a short form for "arrays provide built-in infrastructure to manipulate ordered values in an efficient manner, while hashes do not" After all, when we try to dissuade others from using hashes in some situations, it is not because it is impossible with hashes, but inefficient or awkward. So we can't compare arrays and hashes without looking at the costs to do operations or their built-in access methods. Because without that, i.e. "in theory" perl hashes and arrays are equivalent/just storage/interchangeable UPDATE: changed a really idiotic length(@a) to scalar(@a).
In Section
Meditations
|
|