Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^9: What makes an array sorted and a hash unsorted?

by herveus (Prior)
on Jun 05, 2009 at 14:08 UTC ( [id://768805]=note: print w/replies, xml ) Need Help??


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

Howdy!

When you obtain the list of keys or values from a hash, you no longer have a hash. You have a list, which has an intrinsic order. The mechanism for extracting that list from the hash must traverse the internal data structures in some order to produce the list, but that sequence is explicitly undefined. That is the essence of being unordered.

It makes no sense to say "A hash could return its values in the same order as an array." The statement presupposes that you can predict the order of the list of keys. You can't do that.

If your collection imposes a predictable ordering on the indexes/keys, you can iterate over it directly. Iteration relies on order. That's why iterating over the contents of a hash requires helper functions to manage it. Something has to convert the set of key-value pairs into a list. The resulting list may be invisible to the user (as in each()), or explicitly returned (keys(), values()), but it's there. Saying (%foo) also converts the hash into a list. It's no longer a hash at that point.

Operations over collections, whether ordered or not, may wish to impose an externally defined ordering on a list of the data. That is independent of whether the collection itself is intrinsically ordered. If you want your collection to maintain the data in a specific order, you are stuck with an array, if you intend to use basic Perl data structures. Linked lists and the like are outside the scope of this discourse. Alternately, you impose the desired order on a list each time you generate the list.

To an extent, it really is that simple.

yours,
Michael
  • Comment on Re^9: What makes an array sorted and a hash unsorted?

Replies are listed 'Best First'.
Re^10: What makes an array sorted and a hash unsorted?
by BrowserUk (Patriarch) on Jun 05, 2009 at 15:07 UTC
    the hash must traverse the internal data structures in some order to produce the list, but that sequence is explicitly undefined. That is the essence of being unordered.

    I've been watching from the sidelines and for the most part, this seems like a prime example of what killed the Pedants' Conference--they died arguing about whether the ' should be before or after the s--but I've always enjoyed a good freindly debate, so here goes nothing :)

    I would say that for all hash implementations, including Perl's current, (and all previous) implementations, they do have an "intrisic ordering". That ordering is a definable and concrete function of 2 factors:

    • The hashing algorithm.
    • The bucket size at the point of traversal.

    And given knowledge of algorithm (use the source), and the current bucket size, (see the second element of scalar %hash), the ordering is knowable; therefore definable; therefore defined by the implementation(s).

    However, I agree with the earlier statement--I've lost track of who made it (first)--that there is a significant difference between "order(|ing|ed)" and "sort(|ing|ed)".

    And the answer to the OP question is:

    Whilst the iteration order of arrays is, (for most (all?) languages), unspecified, it is defined, (by convention), to be the same as the natural ordering of its integer indexes; whereas, the iteration order of hashes, (also unspecified), is defined, (as a matter of practicality), to be the simplest or most efficient, (which are often the same thing), order of traversal of the data-structures underlying the implementation.

    As hash implementations differ, the ordering differs with implementation; whereas the natural ordering of integer indexes does not.

    QED.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      And given knowledge of algorithm (use the source), and the current bucket size, (see the second element of scalar %hash), the ordering is knowable; therefore definable; therefore defined by the implementation(s).

      Actually, you have overlooked the order of items on the linked list of a bucket that holds more than one entry.

      - tye        

        Woe! A non-pedantic counter-argument. I wasn't expecting that. But then, nobody expects....

        And you are, as usual, correct.

        I could argue that "knowledge of the algorithm" covers it, but in truth, I hadn't even considered it :)

        But then again, I did say:

        the iteration order of hashes, (also unspecified), is defined, (as a matter of practicality), to be the simplest or most efficient, (which are often the same thing), order of traversal of the data-structures underlying the implementation.

        which absolutlely does cover it. So, halvsies :)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://768805]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found