From: "Ara.T.Howard" Date: 2004-03-04T07:04:44+09:00 Subject: Re: why are Hashes so unsorted? what's your solution? On Thu, 4 Mar 2004, Ruby Baby wrote: > 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 > > > > > 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. :) > > > -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================