John_Dale
(John Dale)
1
New to ruby..
I created a hash array and everything works fine except I can't figure
out how to include a counter to return the first 10 results only.
Thanks John
Here is the code
h = Hash.new
good_words.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
# sort the hash by value, and then print it in this sorted order
h.sort{|a,b| b[1]<=>a[1]}.each { |elem|
puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
}
···
--
Posted via http://www.ruby-forum.com/.
John_Dale
(John Dale)
2
John Dale wrote:
New to ruby..
I created a hash array and everything works fine except I can't figure
out how to include a counter to return the first 10 results only.
Thanks John
Here is the code
h = Hash.new
good_words.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
# sort the hash by value, and then print it in this sorted order
h.sort{|a,b| b[1]<=>a[1]}.each { |elem|
puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
}
referred to an earlier post and figured this out.
Thanks
John
···
--
Posted via http://www.ruby-forum.com/\.
Robert_K1
(Robert K.)
3
Like?
h = Hash.new 0
...
h[w] += 1
...
puts h.sort_by {|w,c| -c}[0,10]
Kind regards
robert
···
On 24.02.2010 20:52, John Dale wrote:
John Dale wrote:
New to ruby..
I created a hash array and everything works fine except I can't figure
out how to include a counter to return the first 10 results only.
Thanks John
Here is the code
h = Hash.new
good_words.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
# sort the hash by value, and then print it in this sorted order
h.sort{|a,b| b[1]<=>a[1]}.each { |elem|
puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
}
referred to an earlier post and figured this out.
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/