On Hashes - How the hashes printing works?

Discussion in 'Ruby' started by Neela megha shyam Chivukula, May 27, 2009.

  1. 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/.
     
    Neela megha shyam Chivukula, May 27, 2009
    #1
    1. Advertising

  2. 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/.
     
    Markus Schirp, May 27, 2009
    #2
    1. Advertising

  3. Neela megha shyam Chivukula, May 27, 2009
    #3
  4. Jarmo Pertman, May 28, 2009
    #4
  5. 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/.
    >
     
    Markus Schirp, May 28, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    591
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    185
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    295
    Matthias Wächter
    Sep 13, 2007
  4. Matt Brooks
    Replies:
    16
    Views:
    252
    Matt Brooks
    Sep 16, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    237
Loading...

Share This Page