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

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

by Roy Johnson (Monsignor)
on Jun 01, 2009 at 17:45 UTC ( #767337=note: print w/ replies, xml ) Need Help??


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

Yes, they'd be considered sorted. However, it would be of limited use, which is probably why it isn't built that way. Arrays are designed for things that are sorted. Hashes are designed for lookup by key, with ordering left up to the users as a separate step.

When people think they have a hash in a particular order, it isn't generally ASCIIbetical order, but the order they put the elements into it. Obviously, that's a misconception. Hashes don't preserve order. Arrays, on the other hand, do. Wherever you put an element, it has a position relative to other elements of the array. That's intrinsic to the array.

All the iterators built into the language will walk the array in the same order. That is not the case for hash iterators. The language could have been written to iterate over arrays in random order, or hashes in predefined order, but it was not.


Caution: Contents may have been coded under pressure.


Comment on Re^3: What makes an array sorted and a hash unsorted?
Re^4: What makes an array sorted and a hash unsorted?
by ikegami (Pope) on Jun 01, 2009 at 17:54 UTC

    Yes, they'd be considered sorted.

    Even with that change, I think we'll still get EXACTLY the same questions we get today. People want custom ordering of keys when dealing with hashes (as opposed to custom ordering of values when dealing with arrays).

    So if your claim is true, that means the answer we currently give ("hashes are unsorted") is wrong because the hashes being sorted wouldn't help at all.

    I actually believe that hash are unsorted, and I believe the answer we are giving is right.

    When people think they have a hash in a particular order, it isn't generally ASCIIbetical order, but the order they put the elements into it.

    Exactly. It's how we use hashes that makes them unsorted. The keys are actually part of the value, and thus can't be used for ordering.

      "Hashes are not sorted the way you think they are sorted" would be correct today as well as with your change. However, it is also (theoretically) possible to implement hashes that retain an order unrelated to their keys. If that were implemented, it would "help", and so the problem is that hashes are unsorted -- in that way.

      I still contend that it is the language that makes them unsorted. It is not merely how we use them. Order has to be imposed externally on the data. There is no intrinsic ordering.


      Caution: Contents may have been coded under pressure.

        So hashes aren't ordered because the following doesn't work?

        $h{foo} = 'a'; $h{bar} = 'b'; $h{baz} = 'c'; is(join(' ', values(%h)), "a b c");

        I guess arrays aren't ordered either, then.

        $a[2] = 'a'; $a[0] = 'b'; $a[1] = 'c'; is("@a", "a b c");

        Order has to be imposed externally on the data in the case of arrays too. It's just there's a convenient way of doing that when the keyspace isn't sparse (namely, the range operator).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2014-04-18 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls