Forum: Ruby How to retrive first 10 items from hash in ruby?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3832c46fac01379dfa5b14c60fac8b5c?d=identicon&s=25 Vikas Gholap (vikas)
on 2009-02-13 12:05
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
D8914caf9cc67403f1f153e0a2104293?d=identicon&s=25 Dave Baldwin (Guest)
on 2009-02-13 13:10
(Received via mailing list)
On 13 Feb 2009, at 11:04, Vikas Gholap wrote:

> 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
A87f7a014c624587fab0d3d78c5b9c18?d=identicon&s=25 BIl Kleb (Guest)
on 2009-02-13 13:26
(Received via mailing list)
Dave Baldwin wrote:
>
> ary[0, 10] will return an array of the first 10 entries

Or, alternatively,

  ary.first(10)

Regards,
F778607e066c8372ab94b0920ed7b2b8?d=identicon&s=25 Jakub Pavlík jn. (Guest)
on 2009-02-13 13:35
(Received via mailing list)
>
>
> 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)
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-13 13:53
(Received via mailing list)
Hi,

Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
> 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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-13 14:04
(Received via mailing list)
Hi --

On Fri, 13 Feb 2009, Bertram Scharpf wrote:

>> 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[i] }


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!
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-13 14:33
(Received via mailing list)
Hi,

Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
>>
>>  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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-13 15:31
(Received via mailing list)
Hi --

On Fri, 13 Feb 2009, Bertram Scharpf wrote:

>>>>
> 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[i] ... }


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!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-13 16:20
(Received via mailing list)
2009/2/13 Bertram Scharpf <lists@bertram-scharpf.de>:
>>>>
> 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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-13 16:27
(Received via mailing list)
Hi --

On Sat, 14 Feb 2009, Robert Klemme wrote:

>>>>> ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])}
>> 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!
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-13 17:39
(Received via mailing list)
Hi,

Am Freitag, 13. Feb 2009, 23:29:05 +0900 schrieb David A. Black:
>   10.times {|i| # ary[i] ... }
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
This topic is locked and can not be replied to.