Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by ikegami (Patriarch)
on Jun 01, 2009 at 17:35 UTC ( [id://767333]=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?

If you say values %hash to get the values of a hash, they will be in no particular order.

So if the following change was built into Perl, hashes would be considered sorted?

use subs qw( values ); sub values(\%) { my $h = shift; return keys(%$h) if !wantarray(); my @keys = sort keys(%$h); return @{$h}{@keys}; }
my %h = map { $_ => $_ } 'a'..'z'; print(values(%h), "\n"); # abcdefghijklmnopqrstuvwxyz

I think there's more to it than just this change in the builtin accessors.

Replies are listed 'Best First'.
Re^3: What makes an array sorted and a hash unsorted?
by Roy Johnson (Monsignor) on Jun 01, 2009 at 17:45 UTC
    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.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-18 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found