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"
}
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
On 24.02.2010 20:52, John Dale wrote:
h = Hash.new
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.
Like?
h = Hash.new 0
…
h[w] += 1
…
puts h.sort_by {|w,c| -c}[0,10]
Kind regards
robert