From: Rick DeNatale Date: 2009-02-12T06:31:20+09:00 Subject: Re: Array uniq --00163646c580ca00370462ab57b6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Wed, Feb 11, 2009 at 3:51 PM, Stuart Little < stuart_clarke1986@hotmail.co.uk> wrote: > I am having some troubles with the uniq method for arrays. This is a > typical example of my data set, which I want to remove the duplicates > from. > > TueAug052008 > TueAug052008 > TueAug052008 > TueAug052008 > > Its clear to see the set of data above has 4 identical values and should > therefore only have remaining after applying array.uniq to this array. > It has become obvious this doesn't work and uniq doesn't remove any > duplicates it just pushes the data out as it finds it full of > dupelictes. Is this a problem with the data set? This is my code: > > value= timeDone.to_s > dateCheck.push value.gsub(/\s/, '')[0..7] + value[26..30] > date = dateCheck.uniq > @alerts.push(date) > > Many thanks > It's hard to say what your code does without more context, but... Here's an attempt to get at the essence of your logic value = "Hi" dateCheck = [] alerts = [] 5.times do dateCheck.push value puts "dateCheck is now #{dateCheck.inspect}" date = dateCheck.uniq puts "date is #{date.inspect}" alerts.push(date) end puts "at end dateCheck is #{dateCheck.inspect}" puts "and alerts is #{alerts.inspect}" which produces: dateCheck is now ["Hi"] date is ["Hi"] dateCheck is now ["Hi", "Hi"] date is ["Hi"] dateCheck is now ["Hi", "Hi", "Hi"] date is ["Hi"] dateCheck is now ["Hi", "Hi", "Hi", "Hi"] date is ["Hi"] dateCheck is now ["Hi", "Hi", "Hi", "Hi", "Hi"] date is ["Hi"] at end dateCheck is ["Hi", "Hi", "Hi", "Hi", "Hi"] and alerts is [["Hi"], ["Hi"], ["Hi"], ["Hi"], ["Hi"]] Maybe this is more like what you want? value = "Hi" dateCheck = [] alerts = [] 5.times do dateCheck.push value puts "dateCheck is now #{dateCheck.inspect}" dateCheck.uniq! puts "dateCheck is now #{dateCheck.inspect}" end puts "at end dateCheck is #{dateCheck.inspect}" dateCheck is now ["Hi"] dateCheck is now ["Hi"] dateCheck is now ["Hi", "Hi"] dateCheck is now ["Hi"] dateCheck is now ["Hi", "Hi"] dateCheck is now ["Hi"] dateCheck is now ["Hi", "Hi"] dateCheck is now ["Hi"] dateCheck is now ["Hi", "Hi"] dateCheck is now ["Hi"] at end dateCheck is ["Hi"] -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale --00163646c580ca00370462ab57b6--