Forum: Ruby Access Hash in the same order that was created

97334b75c93574ed3e514f33849f0953?d=identicon&s=25 Mario Ruiz (tcblues)
on 2008-05-15 18:22
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2008-05-15 18:29
Mario Ruiz wrote:
> I need to access to a Hash in the same order that It was created:
> is that possible?
>
> Thanks

No.
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2008-05-15 18:34
(Received via mailing list)
On May 15, 2008, at 10:22 AM, Mario Ruiz wrote:

> }
> three
> four
>
>
> is that possible?
>
> Thanks




gem install orderedhash

mh = OrderedHash.new
...
...


etc.

a @ http://codeforpeople.com/
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2008-05-15 18:35
(Received via mailing list)
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/
C5d3ed543aa6ca59843aaca55f9ee2c1?d=identicon&s=25 Tor Erik Linnerud (toreriklinnerud)
on 2008-05-15 18:37
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
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2008-05-15 18:40
(Received via mailing list)
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
397f61cc4458e13157b4facf72325e5f?d=identicon&s=25 Gennady Bystritsky (Guest)
on 2008-05-15 18:41
(Received via mailing list)
> mh["two"]="2"
> one
> four
>
> and I would like to get:
> one
> two
> three
> four
>
>
> is that possible?

No way with standard Hash, and it shouldn't be really. You can use an
array instead of a hash, and Array#assoc to retrieve entries based on
keys.

Gennady.
1d53b088a989e069b94597c282eebbbc?d=identicon&s=25 Simon Krahnke (Guest)
on 2008-05-15 19:37
(Received via mailing list)
* Mario Ruiz <mario@betware.com> (18:22) schrieb:

> }
> three
> four
>
>
> is that possible?

The Hash doesn't remember the order in which you put the values in.

If the order is important not easily reconstructable, you have to
store the keys in an Array.

mfg,                      simon .... l
97334b75c93574ed3e514f33849f0953?d=identicon&s=25 Mario Ruiz (tcblues)
on 2008-05-16 11:52
Thanks to everybody
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.