Ryan Mckenzie wrote in post #1012277:
> Ok, so far at the minute then I have included the counter hash so I have
> the following:
>
>   def count_frequency
>     the_file='D:/Rails/projects/data.txt'
>     h = Hash.new
>     words = Hash.new
>     f = File.open(the_file, "r")
>     counts = Hash.new 0
>
>
>
>     if.each do |line|
>       phrase = line.scan /\w+/
>       limit = phrase.length - 1
>
>
>       0.upto limit do |start|
>         start.upto limit do |stop|
>           puts phrase[start..stop].join(' ')
>         end
>       end
>
>        counts.sort_by {|w,c| -c}.each do |w,c|
>          printf "%6d %s\n", c, w
>        end
>     end
>   end
>
> Would the counter hash with the key go underneath the 'puts' in the loop
> so that it records each step? At the minute it still just outputs the
> new strings without the ordering.

At the moment I would be surprised to see any output from counts because 
you never update it.  You also do not close the file properly (you could 
make your life easier by using File.foreach) and I believe there is also 
a spelling error ("if.each").  Did this program actually run and work?

Btw, the_file should rather be a method argument IMHO.

Kind regards

robert

-- 
Posted via http://www.ruby-forum.com/.