jrgifford
7/29/2012 - 11:20 PM

count.rb

h = Hash.new
f = File.open('numbers.csv', "r")
f.each_line { |line|
  numbers = line.split
  numbers.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| a[1]<=>b[1]}.each { |elem|
  puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
}