How to retrive first 10 items from hash in ruby?

Discussion in 'Ruby' started by Vikas Gholap, Feb 13, 2009.

  1. Vikas Gholap

    Vikas Gholap Guest

    Hello All, I am new to programming, i have little problem with hash.

    I want to retrieve first 10 items(key value pairs) from given hash{}

    hashTable = {"a" => 1, "b" => 5, "c" => 2, "d" => 6, "e" => 4, "f" => 7,
    "g" => 9, "h" => 5, "i" => 1, "j" => 8, "k" => 9, "l" => 3, "m" => 7,
    "n" => 10, "o" => 12}

    #sort hashTable on value by descending order
    hashTable.sort {|a,b| -1*(a[1]<=>b[1])}

    #now I want 10 items from sorted hashTable

    I wrote some thing like
    ---------------------------------
    hashTable.each do |key,value|
    puts"Key: #{key} ==> #{value}"
    # I want only first 10
    end
    ------------------------------------
    it prints all elements but i want only 10 items. please help.

    Thanks,
    Vikas
    --
    Posted via http://www.ruby-forum.com/.
    Vikas Gholap, Feb 13, 2009
    #1
    1. Advertising

  2. Vikas Gholap

    Dave Baldwin Guest

    On 13 Feb 2009, at 11:04, Vikas Gholap wrote:

    > Hello All, I am new to programming, i have little problem with hash.
    >
    > I want to retrieve first 10 items(key value pairs) from given hash{}
    >
    > hashTable = {"a" => 1, "b" => 5, "c" => 2, "d" => 6, "e" => 4, "f"
    > => 7,
    > "g" => 9, "h" => 5, "i" => 1, "j" => 8, "k" => 9, "l" => 3, "m" => 7,
    > "n" => 10, "o" => 12}
    >
    > #sort hashTable on value by descending order
    > hashTable.sort {|a,b| -1*(a[1]<=>b[1])}


    You need to save the results.
    ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}

    ary is an array of key value pairs stored in an array, i.e. [['o' =>
    12], ['n' => 11], ...]

    ary[0, 10] will return an array of the first 10 entries

    To print out the first 10 entries:

    ary[0, 10].each {|e| puts "Key: #{e[0} ==> #{[e[1]}"

    Dave
    >
    >
    > #now I want 10 items from sorted hashTable
    >
    > I wrote some thing like
    > ---------------------------------
    > hashTable.each do |key,value|
    > puts"Key: #{key} ==> #{value}"
    > # I want only first 10
    > end
    > ------------------------------------
    > it prints all elements but i want only 10 items. please help.
    >
    > Thanks,
    > Vikas
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Dave Baldwin, Feb 13, 2009
    #2
    1. Advertising

  3. Vikas Gholap

    BIl Kleb Guest

    BIl Kleb, Feb 13, 2009
    #3
  4. > Hello All, I am new to programming, i have little problem with hash.
    >
    > I want to retrieve first 10 items(key value pairs) from given hash{}
    >
    > hashTable = {"a" => 1, "b" => 5, "c" => 2, "d" => 6, "e" => 4, "f" => 7,
    > "g" => 9, "h" => 5, "i" => 1, "j" => 8, "k" => 9, "l" => 3, "m" => 7,
    > "n" => 10, "o" => 12}
    >
    > #sort hashTable on value by descending order
    > hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
    >
    > #now I want 10 items from sorted hashTable
    >
    > I wrote some thing like
    > ---------------------------------
    > hashTable.each do |key,value|
    > puts"Key: #{key} ==> #{value}"
    > # I want only first 10
    > end
    > ------------------------------------
    > it prints all elements but i want only 10 items. please help.
    >
    > Thanks,
    > Vikas
    > --
    > Posted via http://www.ruby-forum.com/.


    i = 0
    hashTable.each_pair {|key,value|
    break if i >= 10
    puts "Key: ..."
    i+=1
    }

    Is this what you need?

    --
    "Configure complete, now type 'make' and PRAY."

    (configure script of zsnes - www.zsnes.com)
    Jakub Pavlík jn., Feb 13, 2009
    #4
  5. Hi,

    Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
    >
    > On 13 Feb 2009, at 11:04, Vikas Gholap wrote:
    >
    >> I want to retrieve first 10 items(key value pairs) from given hash{}
    >>
    >> hashTable.sort {|a,b| -1*(a[1]<=>b[1])}

    >
    > You need to save the results.
    > ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
    >
    > ary[0, 10] will return an array of the first 10 entries
    >
    > ary[0, 10].each {|e| puts "Key: #{e[0} ==> #{[e[1]}"


    As ary is only of temporary use, you don't need to create another
    array ary[0,10]. I suggest:

    10.times { puts "Key: %s ==> %s" % e.shift }
    e.clear # optional

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Feb 13, 2009
    #5
  6. Hi --

    On Fri, 13 Feb 2009, Bertram Scharpf wrote:

    > Hi,
    >
    > Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
    >>
    >> On 13 Feb 2009, at 11:04, Vikas Gholap wrote:
    >>
    >>> I want to retrieve first 10 items(key value pairs) from given hash{}
    >>>
    >>> hashTable.sort {|a,b| -1*(a[1]<=>b[1])}

    >>
    >> You need to save the results.
    >> ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
    >>
    >> ary[0, 10] will return an array of the first 10 entries
    >>
    >> ary[0, 10].each {|e| puts "Key: #{e[0} ==> #{[e[1]}"

    >
    > As ary is only of temporary use, you don't need to create another
    > array ary[0,10]. I suggest:
    >
    > 10.times { puts "Key: %s ==> %s" % e.shift }


    Do you mean ary.shift?

    > e.clear # optional


    You can also save on shifts like this:

    10.times {|i| # do something with ary }


    David

    --
    David A. Black / Ruby Power and Light, LLC
    Ruby/Rails consulting & training: http://www.rubypal.com
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

    http://www.wishsight.com => Independent, social wishlist management!
    David A. Black, Feb 13, 2009
    #6
  7. Hi,

    Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
    > On Fri, 13 Feb 2009, Bertram Scharpf wrote:
    >> Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
    >>> On 13 Feb 2009, at 11:04, Vikas Gholap wrote:
    >>>
    >>>> I want to retrieve first 10 items(key value pairs) from given hash{}
    >>>>
    >>> ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
    >>>

    >> As ary is only of temporary use, you don't need to create another
    >> array ary[0,10]. I suggest:
    >>
    >> 10.times { puts "Key: %s ==> %s" % e.shift }

    >
    > Do you mean ary.shift?


    Arrgh. Of course.

    The code may still be improved: Omit the multiplication by -1 in
    the sort block.

    ary = hashTable.sort { |a,b| a[1]<=>b[1] }
    10.times { puts "Key: %s ==> %s" % ary.pop }

    Bertram

    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Feb 13, 2009
    #7
  8. Hi --

    On Fri, 13 Feb 2009, Bertram Scharpf wrote:

    > Hi,
    >
    > Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
    >> On Fri, 13 Feb 2009, Bertram Scharpf wrote:
    >>> Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
    >>>> On 13 Feb 2009, at 11:04, Vikas Gholap wrote:
    >>>>
    >>>>> I want to retrieve first 10 items(key value pairs) from given hash{}
    >>>>>
    >>>> ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
    >>>>
    >>> As ary is only of temporary use, you don't need to create another
    >>> array ary[0,10]. I suggest:
    >>>
    >>> 10.times { puts "Key: %s ==> %s" % e.shift }

    >>
    >> Do you mean ary.shift?

    >
    > Arrgh. Of course.
    >
    > The code may still be improved: Omit the multiplication by -1 in
    > the sort block.
    >
    > ary = hashTable.sort { |a,b| a[1]<=>b[1] }
    > 10.times { puts "Key: %s ==> %s" % ary.pop }


    I'd still rather walk through the array than have to coordinate the
    sort operation and a destructive array operation.

    ary = hash_table.sort {|a,b| b[1] <=> a[1] }
    # or sort_by {|a| -a[1]}
    10.times {|i| # ary ... }


    David

    --
    David A. Black / Ruby Power and Light, LLC
    Ruby/Rails consulting & training: http://www.rubypal.com
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

    http://www.wishsight.com => Independent, social wishlist management!
    David A. Black, Feb 13, 2009
    #8
  9. 2009/2/13 Bertram Scharpf <>:
    > Hi,
    >
    > Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
    >> On Fri, 13 Feb 2009, Bertram Scharpf wrote:
    >>> Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
    >>>> On 13 Feb 2009, at 11:04, Vikas Gholap wrote:
    >>>>
    >>>>> I want to retrieve first 10 items(key value pairs) from given hash{}
    >>>>>
    >>>> ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
    >>>>
    >>> As ary is only of temporary use, you don't need to create another
    >>> array ary[0,10]. I suggest:
    >>>
    >>> 10.times { puts "Key: %s ==> %s" % e.shift }

    >>
    >> Do you mean ary.shift?

    >
    > Arrgh. Of course.
    >
    > The code may still be improved: Omit the multiplication by -1 in
    > the sort block.
    >
    > ary = hashTable.sort { |a,b| a[1]<=>b[1] }
    > 10.times { puts "Key: %s ==> %s" % ary.pop }


    Did we have these already?

    p hash_table.sort_by {|k,v| -v}.first(10)
    p hash_table.sort_by {|k,v| v}.last(10).reverse

    Cheers

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    Robert Klemme, Feb 13, 2009
    #9
  10. Hi --

    On Sat, 14 Feb 2009, Robert Klemme wrote:

    > 2009/2/13 Bertram Scharpf <>:
    >> Hi,
    >>
    >> Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
    >>> On Fri, 13 Feb 2009, Bertram Scharpf wrote:
    >>>> Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
    >>>>> On 13 Feb 2009, at 11:04, Vikas Gholap wrote:
    >>>>>
    >>>>>> I want to retrieve first 10 items(key value pairs) from given hash{}
    >>>>>>
    >>>>> ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
    >>>>>
    >>>> As ary is only of temporary use, you don't need to create another
    >>>> array ary[0,10]. I suggest:
    >>>>
    >>>> 10.times { puts "Key: %s ==> %s" % e.shift }
    >>>
    >>> Do you mean ary.shift?

    >>
    >> Arrgh. Of course.
    >>
    >> The code may still be improved: Omit the multiplication by -1 in
    >> the sort block.
    >>
    >> ary = hashTable.sort { |a,b| a[1]<=>b[1] }
    >> 10.times { puts "Key: %s ==> %s" % ary.pop }

    >
    > Did we have these already?
    >
    > p hash_table.sort_by {|k,v| -v}.first(10)
    > p hash_table.sort_by {|k,v| v}.last(10).reverse


    I think the original split between the sort and the puts was because
    the main question was about sort, and the puts was just to examine the
    results.


    David

    --
    David A. Black / Ruby Power and Light, LLC
    Ruby/Rails consulting & training: http://www.rubypal.com
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

    http://www.wishsight.com => Independent, social wishlist management!
    David A. Black, Feb 13, 2009
    #10
  11. Hi,

    Am Freitag, 13. Feb 2009, 23:29:05 +0900 schrieb David A. Black:
    > On Fri, 13 Feb 2009, Bertram Scharpf wrote:
    >>
    >> ary = hashTable.sort { |a,b| a[1]<=>b[1] }
    >> 10.times { puts "Key: %s ==> %s" % ary.pop }

    >
    > I'd still rather walk through the array than have to coordinate the
    > sort operation and a destructive array operation.
    >
    > ary = hash_table.sort {|a,b| b[1] <=> a[1] }
    > # or sort_by {|a| -a[1]}
    > 10.times {|i| # ary ... }


    By the way: This is starting to make fun. I don't know how the
    internal sort mechanism works, but maybe something like this here
    would be faster?

    a = Array.new 200 do rand 0x1000 end
    h = []
    a.each { |x|
    if h.length < 10 or x > h.first then
    h.push x
    h.sort!
    h.shift if h.length > 10
    end
    }

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Feb 13, 2009
    #11
    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. weiwei
    Replies:
    0
    Views:
    1,013
    weiwei
    Jan 5, 2007
  2. Amit Shinde

    Retrive the items in list box

    Amit Shinde, May 20, 2004, in forum: ASP General
    Replies:
    1
    Views:
    106
    Dominique
    May 20, 2004
  3. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    614
    David A. Black
    Jul 2, 2008
  5. Ak Newfish
    Replies:
    4
    Views:
    246
    Ak Newfish
    Mar 24, 2009
Loading...

Share This Page