Why are Hashes so unsorted? what's your solution?

From: Ruby Baby [mailto:ruby@hitmedia.com]

In PHP, this is no problem. I get the hash, and I can do the
“each” iteration on it, and get my top-selling albums in order.

In Ruby, it’s an unordered jumble.

Why is that?

And what do you think is the Ruby way solution for what I need to do?

Simplest solution: use an ordered hash implementation, such as rbtree
(http://raa.ruby-lang.org/list.rhtml?name=ruby-rbtree).

Hashes are unordered pretty much by definition (a convenient definition can
be found at Hash table - Wikipedia). Its possible that PHP
hashes, by default, are rb-tree based.

David Naseby
http://homepages.ihug.com.au/~naseby/

Hello David,

Wednesday, March 3, 2004, 6:33:01 AM, you wrote:

Simplest solution: use an ordered hash implementation, such as rbtree

Please don't use this term "ordered hash" anymore. It is just stupid
wrong. You may refer to a "hash" or a "tree" (balanced or not). If
someone asking about an ordered hash just point out that he is
expecting something that does not exist in the known universe.

···

--
Best regards,
Lothar mailto:mailinglists@scriptolutions.com

The problem in terms here is that PHP blurs the lines between hashes,
arrays, whatever you want to call them. There is just one construct,
which they call an Array. Each item in the array can have both a hash
key (a string) and an index (an integer). if you write “myarray[‘test’]
= item”, an index automatically gets created in addition to the hash
key.
Additionally, PHP arrays have an order, which is not necessarily the
same as the order given by the indices. In fact, it is very often
different. Usually it is the order in which items were added to the
array. Calling “each()” iterates over the array in the hidden order. If
you want, you can sort the array/hash by the internal/hidden ordering,
and indices and hashes are unchanged, but iterating over it with
“each()” is different.

Of course, this sounds confusing all thrown together like this, but it
really isn’t that bad. You can do some nifty tricks, all because of the
strange hybrid php hash/lists. :slight_smile:

–Mark

···

On Mar 2, 2004, at 11:33 PM, Lothar Scholz wrote:

Hello David,

Wednesday, March 3, 2004, 6:33:01 AM, you wrote:

Simplest solution: use an ordered hash implementation, such as
rbtree

Please don’t use this term “ordered hash” anymore. It is just stupid
wrong. You may refer to a “hash” or a “tree” (balanced or not). If
someone asking about an ordered hash just point out that he is
expecting something that does not exist in the known universe.

Thanks for your help and advice, everyone.

The description, below, is correct.

Since I learned PHP first, I started heavily relying on this,
not knowing it was a PHP quirk.

So when I learned Ruby, I assumed “Hash” was the same as PHP’s
“associative array” and was surprised they weren’t in order.

···

PHP blurs the lines between hashes,
arrays, whatever you want to call them. There is just one construct,
which they call an Array. Each item in the array can have both a hash
key (a string) and an index (an integer). if you write “myarray[‘test’]
= item”, an index automatically gets created in addition to the hash
key.
Additionally, PHP arrays have an order, which is not necessarily the
same as the order given by the indices. In fact, it is very often
different. Usually it is the order in which items were added to the
array. Calling “each()” iterates over the array in the hidden order. If
you want, you can sort the array/hash by the internal/hidden ordering,
and indices and hashes are unchanged, but iterating over it with
“each()” is different.

Of course, this sounds confusing all thrown together like this, but it
really isn’t that bad. You can do some nifty tricks, all because of the
strange hybrid php hash/lists. :slight_smile:

Thanks for your help and advice, everyone.

The description, below, is correct.

Since I learned PHP first, I started heavily relying on this,
not knowing it was a PHP quirk.

So when I learned Ruby, I assumed “Hash” was the same as PHP’s
“associative array” and was surprised they weren’t in order.

there ARE associtive arrays in ruby if you want…

irb(main):001:0> a=[[:k,:v], [:foo,:bar]]
=> [[:k, :v], [:foo, :bar]]
irb(main):002:0> a.assoc :k
=> [:k, :v]
irb(main):003:0> a.assoc :foo
=> [:foo, :bar]

-a

···

On Thu, 4 Mar 2004, Ruby Baby wrote:

PHP blurs the lines between hashes,
arrays, whatever you want to call them. There is just one construct,
which they call an Array. Each item in the array can have both a hash
key (a string) and an index (an integer). if you write “myarray[‘test’]
= item”, an index automatically gets created in addition to the hash
key.
Additionally, PHP arrays have an order, which is not necessarily the
same as the order given by the indices. In fact, it is very often
different. Usually it is the order in which items were added to the
array. Calling “each()” iterates over the array in the hidden order. If
you want, you can sort the array/hash by the internal/hidden ordering,
and indices and hashes are unchanged, but iterating over it with
“each()” is different.

Of course, this sounds confusing all thrown together like this, but it
really isn’t that bad. You can do some nifty tricks, all because of the
strange hybrid php hash/lists. :slight_smile:

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================