From: "Jesús Gabriel y Galán" Date: 2008-05-22T23:02:29+09:00 Subject: Re: Is rdoc (http://www.ruby-doc.org/core/) complete? On Thu, May 22, 2008 at 3:11 PM, Victor Reyes wrote: > Please forgive my ignorance and thank you for the information. > > I have an array of integers with a minimum of 9 elements and a maximum of > 81. > I need to count the frequency of each digit (1..9). > That's why I was looking into the use of *find_all* or some other util that > would make my code simple. Otherwise I would have to loop and count the old > fashion way. > This is a typical way: irb(main):001:0> a = [1,2,3,4,3,2,1,2,3,4,5,6,5,4,3,4,5,6,7,8,7,8,9] => [1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 5, 6, 5, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9] irb(main):002:0> h = Hash.new {|h,k| h[k] = 0} => {} irb(main):003:0> a.each {|x| h[x] += 1} => [1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 5, 6, 5, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9] irb(main):004:0> h => {5=>3, 6=>2, 1=>2, 7=>2, 2=>3, 8=>2, 3=>4, 9=>1, 4=>4} or: irb(main):005:0> h2 = Hash.new(0) => {} irb(main):006:0> a.each {|x| h2[x] += 1} => [1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 5, 6, 5, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9] irb(main):007:0> h2 => {5=>3, 6=>2, 1=>2, 7=>2, 2=>3, 8=>2, 3=>4, 9=>1, 4=>4} Jesus.