vikas
February 13, 2009, 12:05pm
1
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
vikas
February 13, 2009, 1:10pm
2
On 13 Feb 2009, at 11:04, Vikas G. 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
vikas
February 13, 2009, 1:35pm
4
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)
vikas
February 13, 2009, 2:04pm
5
Hi –
On Fri, 13 Feb 2009, Bertram S. 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 (The Well-Grounded Rubyist )
http://www.wishsight.com => Independent, social wishlist management!
vikas
February 13, 2009, 2:33pm
6
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
vikas
February 13, 2009, 1:53pm
7
Hi,
Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave B.:
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
vikas
February 13, 2009, 3:31pm
8
Hi –
On Fri, 13 Feb 2009, Bertram S. 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 (The Well-Grounded Rubyist )
http://www.wishsight.com => Independent, social wishlist management!
vikas
February 13, 2009, 4:20pm
9
2009/2/13 Bertram S. [email protected] :
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
vikas
February 13, 2009, 4:27pm
10
Hi –
On Sat, 14 Feb 2009, Robert K. 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 (The Well-Grounded Rubyist )
http://www.wishsight.com => Independent, social wishlist management!
vikas
February 13, 2009, 5:39pm
11
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