Access Hash in the same order that was created

M

Mario Ruiz

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
 
A

ara.t.howard

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




gem install orderedhash

mh = OrderedHash.new
...
...


etc.

a @ http://codeforpeople.com/
 
G

Gregory Seidman

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.
--Greg
 
G

Gennady Bystritsky

PiAtLS0tLU9yaWdpbmFsIE1lc3NhZ2UtLS0tLQ0KPiBGcm9tOiBtYXJpb0BiZXR3YXJlLmNvbSBb
bWFpbHRvOm1hcmlvQGJldHdhcmUuY29tXQ0KPiBTZW50OiBUaHVyc2RheSwgTWF5IDE1LCAyMDA4
IDk6MjMgQU0NCj4gVG86IHJ1YnktdGFsayBNTA0KPiBTdWJqZWN0OiBBY2Nlc3MgSGFzaCBpbiB0
aGUgc2FtZSBvcmRlciB0aGF0IHdhcyBjcmVhdGVkDQo+DQo+IEkgbmVlZCB0byBhY2Nlc3MgdG8g
YSBIYXNoIGluIHRoZSBzYW1lIG9yZGVyIHRoYXQgSXQgd2FzIGNyZWF0ZWQ6DQo+DQo+IG1oPUhh
c2gubmV3KCkNCj4gbWhbIm9uZSJdPSIxIg0KPiBtaFsidHdvIl09IjIiDQo+IG1oWyJ0aHJlZSJd
PSIzIg0KPiBtaFsiZm91ciJdPSI0Ig0KPg0KPiBtaC5lYWNoIHt8dmFsfA0KPiAgIHB1dHMgdmFs
WzBdDQo+IH0NCj4NCj4gSW4gdGhpcyBleGFtcGxlIEkgZ2V0Og0KPiB0aHJlZQ0KPiB0d28NCj4g
b25lDQo+IGZvdXINCj4NCj4gYW5kIEkgd291bGQgbGlrZSB0byBnZXQ6DQo+IG9uZQ0KPiB0d28N
Cj4gdGhyZWUNCj4gZm91cg0KPg0KPg0KPiBpcyB0aGF0IHBvc3NpYmxlPw0KDQpObyB3YXkgd2l0
aCBzdGFuZGFyZCBIYXNoLCBhbmQgaXQgc2hvdWxkbid0IGJlIHJlYWxseS4gWW91IGNhbiB1c2Ug
YW4gYXJyYXkgaW5zdGVhZCBvZiBhIGhhc2gsIGFuZCBBcnJheSNhc3NvYyB0byByZXRyaWV2ZSBl
bnRyaWVzIGJhc2VkIG9uIGtleXMuDQoNCkdlbm5hZHkuDQo=
 
T

Tor Erik Linnerud

ara.t.howard said:
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
 
S

Simon Krahnke

* Mario Ruiz said:
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.

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

mfg, simon .... l
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top