Mario Ruiz | 15 May 18:21

Access Hash in the same order that was created

I need to access to a Hash in the same order that It was created:

mh=Hash.new()
mh["one"]="1"
mh["two"]="2"
mh["three"]="3"
mh["four"]="4"

mh.each {|val|
  puts val[0]
}

In this example I get:
three
two
one
four

and I would like to get:
one
two
three
four

is that possible?

Thanks
--

-- 
Posted via http://www.ruby-forum.com/.

(Continue reading)

7stud -- | 15 May 18:29
Picon
Favicon

Re: Access Hash in the same order that was created

Mario Ruiz wrote:
> I need to access to a Hash in the same order that It was created:
> is that possible?
> 
> Thanks

No.
--

-- 
Posted via http://www.ruby-forum.com/.

ara.t.howard | 15 May 18:33
Picon
Gravatar

Re: Access Hash in the same order that was created


On May 15, 2008, at 10:22 AM, Mario Ruiz wrote:

> I need to access to a Hash in the same order that It was created:
>
> mh=Hash.new()
> mh["one"]="1"
> mh["two"]="2"
> mh["three"]="3"
> mh["four"]="4"
>
> mh.each {|val|
>  puts val[0]
> }
>
> In this example I get:
> three
> two
> one
> four
>
> and I would like to get:
> one
> two
> three
> four
>
>
> is that possible?
>
(Continue reading)

Tor Erik Linnerud | 15 May 18:37
Picon
Gravatar

Re: Access Hash in the same order that was created

ara.t.howard wrote:
> is that possible?

Hash preserves insertion order in Ruby 1.9

irb(main):026:0> RUBY_VERSION
=> "1.9.0"
irb(main):027:0> mh.each{|val| puts val[0]}
one
two
three
four

regards,
Tor Erik
--

-- 
Posted via http://www.ruby-forum.com/.

ara.t.howard | 15 May 18:33
Picon
Gravatar

Re: Access Hash in the same order that was created


On May 15, 2008, at 10:22 AM, Mario Ruiz wrote:

> is that possible?

forgot to mention - 1.9 does this by default now.

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama

Gregory Seidman | 15 May 18:35

Re: Access Hash in the same order that was created

On Fri, May 16, 2008 at 01:22:56AM +0900, Mario Ruiz wrote:
> I need to access to a Hash in the same order that It was created:
[...]
> is that possible?

First off, this is not a hash. Hashes are inherently unordered. Hashes
provide amortized O(1) insertion and retrieval of elements by key, and
that's it. If you need an ordered set of pairs, use an array of arrays.
Yes, this is a pet peeve of mine. 

There was an OrderedHash that someone wrote, but needing it is an
indication of a design problem. It has also been obsoleted by the
Dictionary in Ruby Facets.

One could argue that, like Java, there should be a standard Dictionary
interface that Hash implements, and that there should be another
implementation that preserves order without guaranteeing anything about
performance. That's not how things stand right now.

> Thanks
--Greg

Gennady Bystritsky | 15 May 18:36
Picon
Favicon

Re: Access Hash in the same order that was created

> -----Original Message-----
> From: mario <at> betware.com [mailto:mario <at> betware.com]
> Sent: Thursday, May 15, 2008 9:23 AM
> To: ruby-talk ML
> Subject: Access Hash in the same order that was created
>
> I need to access to a Hash in the same order that It was created:
>
> mh=Hash.new()
> mh["one"]="1"
> mh["two"]="2"
> mh["three"]="3"
> mh["four"]="4"
>
> mh.each {|val|
>   puts val[0]
> }
>
> In this example I get:
> three
> two
> one
> four
>
> and I would like to get:
> one
> two
> three
> four
>
(Continue reading)

Simon Krahnke | 15 May 19:35
Picon

Re: Access Hash in the same order that was created

* Mario Ruiz <mario <at> betware.com> (18:22) schrieb:

> I need to access to a Hash in the same order that It was created:
>
> mh=Hash.new()
> mh["one"]="1"
> mh["two"]="2"
> mh["three"]="3"
> mh["four"]="4"
>
> mh.each {|val|
>   puts val[0]
> }
>
> In this example I get:
> three
> two
> one
> four
>
> and I would like to get:
> one
> two
> three
> four
>
>
> is that possible?

The Hash doesn't remember the order in which you put the values in.
(Continue reading)

Mario Ruiz | 16 May 11:52

Re: Access Hash in the same order that was created

Thanks to everybody
--

-- 
Posted via http://www.ruby-forum.com/.


Gmane