15 May 19:35
Re: Access Hash in the same order that was created
From: Simon Krahnke <overlord <at> gmx.li>
Subject: Re: Access Hash in the same order that was created
Newsgroups: gmane.comp.lang.ruby.general
Date: 2008-05-15 17:35:15 GMT
Subject: Re: Access Hash in the same order that was created
Newsgroups: gmane.comp.lang.ruby.general
Date: 2008-05-15 17:35:15 GMT
* 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.
If the order is important not easily reconstructable, you have to
store the keys in an Array.
mfg, simon .... l
RSS Feed