From: dblack@... Date: 2007-02-08T06:30:33+09:00 Subject: Re: histogram of histograms Hi -- On Thu, 8 Feb 2007, Charles L. Snyder wrote: > Hi > > I have several text files that look like this: > > Brazil, 10 > Brazil, 13 > Brazil, 9 > Bulgaria, 1 > Canada, 48 > Canada, 52 > Canada, 38 > Canada, 55 > Canada, 59 > Chile, 1 > Chile, 1 > Chile, 2 > China, 7 > China, 18 > China, 19 > China, 22 > China, 25 > > I need to iterate through the above file(s) and get the data > summarized in the form: > > Canada, 252 > China, 91 > Chile, 4 > Brazil, 32 > Bulgaria, 1 > > I know how to go from a single column list with multiple repeated > values to a 'histogram' type list, ie: > > my_hash = countries.inject(Hash.new { 0 }) { |counts, key| counts[key] > += 1; counts} > my_hash = my_hash.sort { |a,b| a[1] <=> b[1] } my_hash will actually become an array at that point :-) > but I'm unable to figure out how to get the 2-column csv values into a > total by country as shown above. > (I do have another file "countries.txt" which is a unique list of > countries.) Here's one way: require 'scanf' hash = Hash.new {0} DATA.scanf("%s%d") {|key,count| hash[key] += count } hash.sort.each {|k,v| puts "#{k} #{v}" } __END__ Brazil, 10 Brazil, 13 Brazil, 9 Bulgaria, 1 etc. That has the slight ugliness of including the comma in the key. You could do: hash[key.chomp(",")] += count to avoid that, and then add the comma to the printout if you want it back. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)