From: Stuart Clarke Date: 2009-02-04T03:34:51+09:00 Subject: Re: Hash counting Thanks for getting back to me. I have done similar to you in Fxri and got those results earlier it seems you may be correct and eventdateID and id, cnt do not like eachother so much. After doing an inspect on eventdateID array I only get the following: ["WedAug062008"] This is strange as it seems to missing all the other data. For your information in my actual code I do @alerts.push(counts) counts = Hash.new(0) eventdateID.each {|d| counts[d] += 1} @alerts.push(counts) counts.each do |id,cnt| and get what is expected: MonFeb0220091 MonFeb0220091 MonFeb0220091 MonFeb0220091 MonFeb0220091 MonFeb0220091 WedAug062008 WedAug062008 Its the next step counts.each do |id,cnt| which is the problem. > If each element in the array eventDateID is stored in the hash as a > different key (which is what seems to be happening), maybe what is > inside the array are not strings, but another class that has a > different implementation of eql?. Not sure what you mean by this. This is how I make eventdateID, just regular expressions on a string from a struct: eventdateID.push eventsbydate.gsub(/\s/, '')[0..7] + eventsbydate[26..30] Many thanks Jes炭s Gabriel y Gal叩n wrote: > On Tue, Feb 3, 2009 at 6:16 PM, Stuart Clarke > wrote: > >>> WedAug062008 >>>> end >> 1 >> 1 >> 1 >> 1 >> 1 >> > > Sorry Stuart, can you show the exact code that produces that output > (including the puts that you are using to print those values)? Cause > this works for me as it is: > > > irb(main):009:0> eventDateID = %w{MonFeb0220091 MonFeb0220091 > MonFeb0220091 MonFeb0220091 MonFeb0220091 MonFeb0220091 WedAug062008 > WedAug062008} > => ["MonFeb0220091", "MonFeb0220091", "MonFeb0220091", > "MonFeb0220091", "MonFeb0220091", "MonFeb0220091", "WedAug062008", > "WedAug062008"] > irb(main):010:0> counts = Hash.new(0) > => {} > irb(main):011:0> eventDateID.each {|d| counts[d] += 1} > => ["MonFeb0220091", "MonFeb0220091", "MonFeb0220091", > "MonFeb0220091", "MonFeb0220091", "MonFeb0220091", "WedAug062008", > "WedAug062008"] > irb(main):012:0> counts > => {"MonFeb0220091"=>6, "WedAug062008"=>2} > irb(main):013:0> @alerts = [] > => [] > irb(main):014:0> counts.each do |id, cnt| > irb(main):015:1* @alerts.push(id) if cnt >= 5 > irb(main):016:1> end > => {"MonFeb0220091"=>6, "WedAug062008"=>2} > irb(main):017:0> @alerts > => ["MonFeb0220091"] > > > If each element in the array eventDateID is stored in the hash as a > different key (which is what seems to be happening), maybe what is > inside the array are not strings, but another class that has a > different implementation of eql?. > Can you inspect the eventDateID array to check that? > > Jesus. -- Posted via http://www.ruby-forum.com/.