From: "David A. Black" Date: 2009-02-12T07:45:55+09:00 Subject: Re: Array uniq Hi -- On Thu, 12 Feb 2009, Stuart Little wrote: > David A. Black wrote: >> >> On Thu, 12 Feb 2009, Stuart Little wrote: >> >>> Is returned after my code exactly the same, I am expecting >>> >>> WedAug062008 >>> TueAug052008 >>> >>> I have dumbed the code down to see what you think: >>> >>> time = event.time_written.to_s >>> dateCheck = [time.gsub(/\s/, '')[0..7] + time[26..29]] >>> printdata = [dateCheck] >>> @alerts.push printdata.uniq >> >> printdata.uniq is the same items as printdata, because printdata only >> contains one item. So you're doing this: > >> dateCheck = ["string"] >> printdata = [["string"]] >> @alerts.push [["string"]].uniq > >> I don't how deeply nested you want your arrays, but if @alerts is the >> final product and you want it uniq, you could run @alerts.uniq! at the >> end, or do something like: > >> @alerts.push printdata unless @alerts.include?(printdata) > > That makes sense but how do I avoid this? I cant see how. I need to > dedupe before the data goes into @alerts. I'm sorry but I'm getting confused. You've got dateCheck, which is a string, and printdata, which is an array containing only that one string (so .uniq is not an issue). The only point at which you have the possibility of adding non-uniq data to an array is the @alerts.push operation, and I specifically suggesting that you not put the data in there if it's already there. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!