[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to retrive first 10 items from hash in ruby?

Vikas Gholap

2/13/2009 11:04:00 AM

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

10 Answers

Dave Baldwin

2/13/2009 12:08:00 PM

0


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


Bil Kleb

2/13/2009 12:21:00 PM

0

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.lar...
http://twitter.co...

Jakub Pavlík jn.

2/13/2009 12:31:00 PM

0

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

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)

Bertram Scharpf

2/13/2009 12:52:00 PM

0

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

David A. Black

2/13/2009 1:03:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Bertram Scharpf

2/13/2009 1:26:00 PM

0

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

David A. Black

2/13/2009 2:29:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Robert Klemme

2/13/2009 3:18:00 PM

0

2009/2/13 Bertram Scharpf <lists@bertram-scharpf.de>:
> 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

David A. Black

2/13/2009 3:21:00 PM

0

Hi --

On Sat, 14 Feb 2009, Robert Klemme wrote:

> 2009/2/13 Bertram Scharpf <lists@bertram-scharpf.de>:
>> 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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Bertram Scharpf

2/13/2009 4:38:00 PM

0

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[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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...