Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to retrive first 10 items from hash in ruby?

Reply
Thread Tools

How to retrive first 10 items from hash in ruby?

 
 
Vikas Gholap
Guest
Posts: n/a
 
      02-13-2009
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/.

 
Reply With Quote
 
 
 
 
Dave Baldwin
Guest
Posts: n/a
 
      02-13-2009

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



 
Reply With Quote
 
 
 
 
BIl Kleb
Guest
Posts: n/a
 
      02-13-2009
Dave Baldwin wrote:
>
> ary[0, 10] will return an array of the first 10 entries


Or, alternatively,

ary.first(10)

Regards,
--
Bil Kleb
http://fun3d.larc.nasa.gov
http://twitter.com/bil_kleb
 
Reply With Quote
 
Jakub Pavlík jn.
Guest
Posts: n/a
 
      02-13-2009
> 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)

 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      02-13-2009
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

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      02-13-2009
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[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!

 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      02-13-2009
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

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      02-13-2009
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[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!

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-13-2009
2009/2/13 Bertram Scharpf <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      02-13-2009
Hi --

On Sat, 14 Feb 2009, Robert Klemme wrote:

> 2009/2/13 Bertram Scharpf <(E-Mail Removed)>:
>> 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!

 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Windows : Retrive Quick Launch toolbar items Ak Newfish Ruby 4 03-24-2009 04:23 AM
retrive preselected value in second drop down list from the first drop down list weiwei ASP .Net 0 01-05-2007 07:29 PM
Retrive the items in list box Amit Shinde ASP General 1 05-20-2004 01:00 PM



Advertisments