From: John Dale Date: 2010-02-25T04:52:43+09:00 Subject: Re: Create a counter in a hash to return first 10 results only 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/.