From: Robert Klemme Date: 2009-02-03T06:24:48+09:00 Subject: Re: Hash counting On 02.02.2009 21:43, 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 You probably rather want eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] + eventsbydate[26..30] counts = Hash.new(0) eventdateID.each {|d| counts[d] +=1} counts.each do |d,cnt| @alerts.push("#{event.event_id} #{@tab} #{event.time_written} #{@tab}#{event.event_type} #{@tab} #{type}") if cnt >= 5 end Cheers robert