From: Steven Demonnin Date: 2009-06-19T07:33:47+09:00 Subject: Re: need a hash/iteration tutorial...text reading. Matt Neuburg wrote: > 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? > > That is called a "histogram" and is one of the most common examples > (Google is your friend). The sticking point here is what you mean by a > "word". If you're willing to accept a fairly crude definition of this > notion, then that is an example I develop in the Blocks section of my > Ruby tutorial chapter here: > > http://www.apeth.com/ruby/02justenoughruby.html > > To sort, add this line: > > wds = h.sort {|x,y| x[1] <=> y[1]} > > Note that the concept "sort a hash" has no real meaning, since a hash is > not ordered. What you can do is to convert to an array and sort the > array. Since arrays are key/value (from what I can understand), there are only two part to the array. I thought you couldn't put a third value in an array. thanks for the help. I am going to check out the web page. (Never knew of Histogram. Learn something new every other day or so.) -- Posted via http://www.ruby-forum.com/.