[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

On Hashes - How the hashes printing works?

Neela megha shyam Chivukula

5/27/2009 8:17:00 AM

Hi All,

I have my code as follows:-

people = {
"torvalds"=>{"lname"=>"Torvalds", "fname"=>"Linus",
"job"=>"maintainer"}, "matsumoto"=>{"lname"=>"Matsumoto",
"fname"=>"Yukihiro", "job"=>"Ruby originator"},
"litt"=>{"lname"=>"Litt", "fname"=>"Steve", "job"=>"troubleshooter"}
}
keys = people.keys
for key in 0...keys.length
print "key : ", keys[key], "\n"
print "lname: ", people[keys[key]]["lname"], "\n"
print "fname: ", people[keys[key]]["fname"], "\n"
print "job : ", people[keys[key]]["job"], "\n"
print "\n\n"
end

O/p:-
key : litt
lname: Litt
fname: Steve
job : troubleshooter
key : matsumoto
lname: Matsumoto
fname: Yukihiro
job : Ruby originator
key : torvalds
lname: Torvalds
fname: Linus
job : maintainer

Question:-
Why key of "litt" is printed first thought the first key of hash is
"torvalds"?

Thanks and regards,
Neela.
--
Posted via http://www.ruby-....

4 Answers

Markus Schirp

5/27/2009 8:26:00 AM

0

Hashes enumeration does not mirror insertion order.
Hashes are not ordered:

http://en.wikipedia.org/wiki/Hash_table...

Markus


On Wed, May 27, 2009 at 05:16:57PM +0900, Neela megha shyam Chivukula wrote:
> Hi All,
>
> I have my code as follows:-
>
> people = {
> "torvalds"=>{"lname"=>"Torvalds", "fname"=>"Linus",
> "job"=>"maintainer"}, "matsumoto"=>{"lname"=>"Matsumoto",
> "fname"=>"Yukihiro", "job"=>"Ruby originator"},
> "litt"=>{"lname"=>"Litt", "fname"=>"Steve", "job"=>"troubleshooter"}
> }
> keys = people.keys
> for key in 0...keys.length
> print "key : ", keys[key], "\n"
> print "lname: ", people[keys[key]]["lname"], "\n"
> print "fname: ", people[keys[key]]["fname"], "\n"
> print "job : ", people[keys[key]]["job"], "\n"
> print "\n\n"
> end
>
> O/p:-
> key : litt
> lname: Litt
> fname: Steve
> job : troubleshooter
> key : matsumoto
> lname: Matsumoto
> fname: Yukihiro
> job : Ruby originator
> key : torvalds
> lname: Torvalds
> fname: Linus
> job : maintainer
>
> Question:-
> Why key of "litt" is printed first thought the first key of hash is
> "torvalds"?
>
> Thanks and regards,
> Neela.
> --
> Posted via http://www.ruby-....

Neela megha shyam Chivukula

5/27/2009 8:28:00 AM

0

Markus Schirp wrote:
> Hashes enumeration does not mirror insertion order.
> Hashes are not ordered:
>
> http://en.wikipedia.org/wiki/Hash_table...
>
> Markus

Thank you Markus.
--
Posted via http://www.ruby-....

Jarmo Pertman

5/28/2009 6:31:00 AM

0

Jou could use orderedhash gem to have hashes ordered like arrays for
example if you really need it for some reason:
http://codeforpeople.com/lib/ruby/or...

Markus Schirp wrote:
> Hashes enumeration does not mirror insertion order.
> Hashes are not ordered:
>
> http://en.wikipedia.org/wiki/Hash_table...
>
> Markus

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

Markus Schirp

5/28/2009 10:57:00 AM

0

Or maintain an "order of insertion key array".


On Thu, May 28, 2009 at 03:30:42PM +0900, Jarmo Pertman wrote:
> Jou could use orderedhash gem to have hashes ordered like arrays for
> example if you really need it for some reason:
> http://codeforpeople.com/lib/ruby/or...
>
> Markus Schirp wrote:
> > Hashes enumeration does not mirror insertion order.
> > Hashes are not ordered:
> >
> > http://en.wikipedia.org/wiki/Hash_table...
> >
> > Markus
>
> --
> Posted via http://www.ruby-....
>