Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by ikegami (Patriarch)
on Jun 01, 2009 at 17:07 UTC ( [id://767324]=note: print w/replies, xml ) Need Help??


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

The difference is in this code:

print $_ foreach @array; print $_ foreach keys %hash

So if keys sorted the keys before returning them, the hash would be considered sorted? nah.

Another way to look at is that an array keeps it's sort order, where a hash doesn't.

keys may not return the keys in order, but it could. "f" is not suddenly less than "b" because keys decides to return foo before bar.

Again, it can't be that hashes aren't ordered simply because Perl doesn't provide a builtin function to return them sorted like it does for arrays.

The hash's list will change with every new run.

Nit:

s/will/may/

Perl can and will return them in the same order in some circumstances, even after the 5.8.1 change to add randomisation.

$ perl -e'system(perl => "-le %h = map { \$_=>1 } qw(a b c d); print k +eys %h") for 1..10' cabd cabd cabd cabd cabd cabd cabd cabd cabd cabd

Replies are listed 'Best First'.
Re^3: What makes an array sorted and a hash unsorted?
by DStaal (Chaplain) on Jun 01, 2009 at 17:15 UTC

    The point isn't that there is a way to sort them. It's whether you need to use it to guarantee the order you get: With an array you don't, with a hash you do.

    And yes, may. But the point is on the array the order will not. You can be sure of that: That given the same input, you will get the same output. A hash intentionally does not guarantee that.

    And no, sorting the keys does not make the hash sorted. It simply means you have a sorted array that can be used to access the hash in a sorted order.

      The point isn't that there is a way to sort them.

      Of course you can. Just like array indexes are ordered in ascending numeric order, hash keys can be ordered in ascending lexical order.

      You don't see people telling arrays that 5 sorts after 4. No, they change the key-value associations instead. You can do exactly the same thing with hashes. The difference is that people don't want to change the key-value associations for hashes.

      Thus my conclusion: That hashes are unsorted isn't a property of hashes so much as of the way we use them.

Re^3: What makes an array sorted and a hash unsorted?
by shmem (Chancellor) on Jun 01, 2009 at 17:36 UTC
    ... even after the 5.8.1 change to add randomisation.

    So, while not yet perfect, the tendency is to randomize the order in which keys or values are returned. keys and values are meant to return their values in no predictable order.

    You say,

    keys may not return the keys in order, but it could.

    Yes, e.g. tying the hash to DB_File, as DB_BTREE. But what's your point? In theory, it could, but in practice, it doesn't. In which theoretical circumstances that would be meaningful in practice, would keys return the keys of a perl untied hash in any meaningful order (i.e. shortest..longest, lexically sorted, lifo/fifo or such) ? And why should it?

      But what's your point? In theory, it could, but in practice, it doesn't.

      My answer to Roy answers this as well.

      keys and values are meant to return their values in no predictable order.

      No, it's not a feature, it's a side-effect. It would be needlessly costly to return them sorted. Why is it needless? Because people would still want custom sorting of the keys. (With arrays, they want custom sorting of the values.)

Re^3: What makes an array sorted and a hash unsorted?
by John M. Dlugosz (Monsignor) on Jun 01, 2009 at 19:49 UTC
    will vs may:

    I think newer versions of perl WILL, intentionally, as a security feature.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found