From: Michael Kohl Date: 2009-06-19T06:15:09+09:00 Subject: Re: need a hash/iteration tutorial...text reading. --001e680f15940b13fd046ca5e78a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Thu, Jun 18, 2009 at 10:40 PM, Steven Demonnin wrote: > > What I want to know is, is it possible to create a has where the key is > the word, and the value is the number of occurrences of the word in the > text, and then sort the hash by the values? > -- > Posted via http://www.ruby-forum.com/. Super quick and dirty, but should get you started: words = {} File.open("txt.txt").each do |line| line.split(' ').each { |w| words.has_key?(w) ? words[w] += 1 : words[w] = 1 } end words.sort_by { |e| e[1]}.reverse.each { |k, v| puts "#{k}: #{v}"} -- Blog: http://citizen428.net/ Twitter: http://twitter.com/citizen428 --001e680f15940b13fd046ca5e78a--