How to retrive first 10 items from hash in ruby?

V

Vikas Gholap

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
 
D

Dave Baldwin

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
 
J

Jakub Pavlík jn.

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

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

Is this what you need?
 
B

Bertram Scharpf

Hi,

Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
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
 
D

David A. Black

Hi --

Hi,

Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin:
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!
 
B

Bertram Scharpf

Hi,

Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
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
 
D

David A. Black

Hi --

Hi,

Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
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!
 
R

Robert Klemme

2009/2/13 Bertram Scharpf said:
Hi,

Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
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
 
D

David A. Black

Hi --

2009/2/13 Bertram Scharpf said:
Hi,

Am Freitag, 13. Feb 2009, 22:02:31 +0900 schrieb David A. Black:
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!
 
B

Bertram Scharpf

Hi,

Am Freitag, 13. Feb 2009, 23:29:05 +0900 schrieb David A. Black:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top