efficient way to get the first found (or a random) key/value of a large hash

Discussion in 'Ruby' started by David Garamond, Jan 12, 2004.

  1. # a large hash
    h = { ... }

    # 1. this is inefficient
    first_key, first_value = h.keys[0], h.values[0]

    # also, #1 is not thread-safe right?
    # is there a #pairs method which returns [[k1,v1], [k2,v2], ...} or
    # something similar?

    # 2. will this be compatible with future ruby?
    h.each_pair {|k,v| first_key, first_value = k, v; break}

    --
    dave
    David Garamond, Jan 12, 2004
    #1
    1. Advertising

  2. David Garamond

    Hal Fulton Guest

    Re: efficient way to get the first found (or a random) key/valueof a large hash

    David Garamond wrote:
    > # a large hash
    > h = { ... }
    >
    > # 1. this is inefficient
    > first_key, first_value = h.keys[0], h.values[0]
    >
    > # also, #1 is not thread-safe right?
    > # is there a #pairs method which returns [[k1,v1], [k2,v2], ...} or
    > # something similar?
    >
    > # 2. will this be compatible with future ruby?
    > h.each_pair {|k,v| first_key, first_value = k, v; break}



    Hmm, how about:

    k,v = h.find { true } # Find first obj for which block is true


    Hal
    Hal Fulton, Jan 12, 2004
    #2
    1. Advertising

  3. David Garamond

    Ville Aine Guest

    David Garamond <lists@zara.6.isreserved.com> wrote in message news:<40025921.4090603@zara.6.isreserved.com>...

    > # is there a #pairs method which returns [[k1,v1], [k2,v2], ...} or
    > # something similar?


    Hash#to_a does just that:

    irb(main):003:0> {:a => 1, :b => 2}.to_a
    => [[:a, 1], [:b, 2]]
    Ville Aine, Jan 13, 2004
    #3
  4. Re: efficient way to get the first found (or a random) key/valueof a large hash

    Ville Aine wrote:

    >David Garamond <lists@zara.6.isreserved.com> wrote in message news:<40025921.4090603@zara.6.isreserved.com>...
    >
    >
    >># is there a #pairs method which returns [[k1,v1], [k2,v2], ...} or
    >># something similar?
    >>
    >>

    >Hash#to_a does just that:
    >
    >irb(main):003:0> {:a => 1, :b => 2}.to_a
    >=> [[:a, 1], [:b, 2]]
    >
    >
    >

    yes, but converting the whole hash to an array is inefficient (for big
    hashes), that's why he doesn't want to use this.
    Emmanuel Touzery, Jan 14, 2004
    #4
    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. JLeary
    Replies:
    3
    Views:
    178
    Yan-Hong Huang[MSFT]
    Aug 6, 2003
  2. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  3. Une bévue
    Replies:
    5
    Views:
    149
    Une bévue
    Aug 10, 2006
  4. Antonio Quinonez
    Replies:
    2
    Views:
    167
    Antonio Quinonez
    Aug 14, 2003
  5. Ralf Baerwaldt
    Replies:
    1
    Views:
    132
    Paul Lalli
    Jul 20, 2004
Loading...

Share This Page