Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > On Hashes - How the hashes printing works?

Reply
Thread Tools

On Hashes - How the hashes printing works?

 
 
Neela megha shyam Chivukula
Guest
Posts: n/a
 
      05-27-2009
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-forum.com/.

 
Reply With Quote
 
 
 
 
Markus Schirp
Guest
Posts: n/a
 
      05-27-2009
Hashes enumeration does not mirror insertion order.
Hashes are not ordered:

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

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-forum.com/.


 
Reply With Quote
 
 
 
 
Neela megha shyam Chivukula
Guest
Posts: n/a
 
      05-27-2009
Markus Schirp wrote:
> Hashes enumeration does not mirror insertion order.
> Hashes are not ordered:
>
> http://en.wikipedia.org/wiki/Hash_table#Drawbacks
>
> Markus


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

 
Reply With Quote
 
Jarmo Pertman
Guest
Posts: n/a
 
      05-28-2009
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/orderedhash/

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


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

 
Reply With Quote
 
Markus Schirp
Guest
Posts: n/a
 
      05-28-2009
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/orderedhash/
>
> Markus Schirp wrote:
> > Hashes enumeration does not mirror insertion order.
> > Hashes are not ordered:
> >
> > http://en.wikipedia.org/wiki/Hash_table#Drawbacks
> >
> > Markus

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments