From: Ilan Berci Date: 2009-02-03T05:59:08+09:00 Subject: Re: Hash counting Stuart, I believe this will get you closer to what you want.. [1,2,2,3,3,3].inject({}) do |hash, val| hash[val] ||= 0 hash[val] +=1 hash end => {1=>1,2=>2,3=>3} hth ilan Stuart Clarke wrote: > I am trying to load some data into a hash and then count how many times > it occurs in the hash, if it occurs more than 5 times then we are adding > some data to an array. Below is my code which I will explain > > eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] + > eventsbydate[26..30] > counts = Hash.new(0) > if eventdateID.find {|d| (counts[d] +=1) >= 5} > @alerts.push("#{event.event_id} #{@tab} #{event.time_written} > #{@tab}#{event.event_type} #{@tab} #{type}") > end > -- Posted via http://www.ruby-forum.com/.