Re: Get values from hash "in order"



still just me wrote:
[about hashes]
Is there some advantage to Perl in them storing in random order?
Perhaps some internal storage efficiency?

Actually the order in which e.g. each() or keys() cycles through the items
of a hash is not random at all but it is well defined and guaranteed to be
the same for every access to that hash. It is just that this sequence
doesn't make any sense to the programmer because this sequence is -as you
suspected- based on the internal implementation and not on some human
expectation about useful order.

However, on an abstract level, hashes are a generalization of arrays. Arrays
are mappings from natural numbers (assuming you didn't mess with $[) into
scalars while hashes are mappings from arbitrary strings into scalars.
Natural numbers have an inherent sequence, there is a natural next and
previous. Therefore you can specify a concrete point in the array and from
there access the next and the previous element.
There is no such obvious sequence for strings. Alphabetical sorting is not
compact, i.e. between any two given strings there is always another string.
And incremental strings as produced by the ++ operator are not what people
would expect from a sorted hash.

And because there is no such obvious order on the domain elements of hashes
as is are the natural numbers for arrays it doesn't make sense to speak of a
sequence of the elements of hashes. Sequence of order is foreign to hashes.

jue


.



Relevant Pages

  • Re: Hash question
    ... suppose the strings and the hashes are the ... The first hash is a hash of the first string, I want to know whether the ... "difference", including XOR, subtraction, and subtraction ...
    (sci.crypt)
  • Re: How to avoid rehashing?
    ... with strings. ... And keeping this in a hash. ... I use the words as keys since I want to find every occurence of the ... these hashes, which I need to do more than once. ...
    (perl.beginners)
  • Re: Hash question
    ... I have two unknown strings which differ by a known amount, ... suppose the strings and the hashes are the ... The first hash is a hash of the first string, I want to know whether the ...
    (sci.crypt)
  • Re: Recursion limit problems
    ... Dicts first compare hashes and if they are equal, ... two unequal strings have the same hash value, ... (given only 2**32 possible hashes and many more possible strings), ... sentence, about mixing, is directed at minimizing such hash collisions. ...
    (comp.lang.python)
  • Re: Hash question
    ... suppose the strings and the hashes are the ... The first hash is a hash of the first string, I want to know whether the ...
    (sci.crypt)