Array uniq

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

···

--
Posted via http://www.ruby-forum.com/.

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

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)

Array#uniq does work:

a = Array.new(4) { "TueAug052008" }

=> ["TueAug052008", "TueAug052008", "TueAug052008", "TueAug052008"]

a.uniq

=> ["TueAug052008"]

So there must be something else going on. Maybe stray whitespace? How
are you determining that the operation didn't work?

David

···

On Thu, 12 Feb 2009, Stuart Little wrote:

--
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!

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"]

···

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

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale

Use uniq! Which modifies the receiver in place.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

···

On 12/02/2009, at 7:51 AM, 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

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
--
Posted via http://www.ruby-forum.com/\.

Thanks for the response.

I remove whitespace with gsub. My original data set:

TueAug052008
WedAug062008
TueAug052008
WedAug062008
WedAug062008

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

Many thanks

David A. Black wrote:

···

On Thu, 12 Feb 2009, Stuart Little wrote:

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)

Array#uniq does work:

a = Array.new(4) { "TueAug052008" }

=> ["TueAug052008", "TueAug052008", "TueAug052008", "TueAug052008"]

a.uniq

=> ["TueAug052008"]

So there must be something else going on. Maybe stray whitespace? How
are you determining that the operation didn't work?

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!

--
Posted via http://www.ruby-forum.com/\.

I want something simpler than that to be honest. I just want to get rid
of all duplicate entries in the data set and leave just one of each
unique value in my array.

Also, FYI .uniq returns all the values as they are and doesn't remove
duplicates. .uniq! gets rid of all the values and leaves me with no data
at all

···

--
Posted via http://www.ruby-forum.com/.

Stuart, in this code you have an array with exactly one element, so
#uniq returns the original array. Can you add the line "p printdata"
and show us the output?

Regards,
Pit

···

2009/2/11 Stuart Little <stuart_clarke1986@hotmail.co.uk>:

     time = event.time_written.to_s
     dateCheck = [time.gsub(/\s/, '')[0..7] + time[26..29]]
     printdata = [dateCheck]
     @alerts.push printdata.uniq

Hi --

Thanks for the response.

I remove whitespace with gsub. My original data set:

TueAug052008
WedAug062008
TueAug052008
WedAug062008

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)

David

···

On Thu, 12 Feb 2009, Stuart Little wrote:

--
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!

I want something simpler than that to be honest. I just want to get rid
of all duplicate entries in the data set and leave just one of each
unique value in my array.

I guess you are replying to me. Perhaps it would be best not to cut
everything in the reply.

Also, FYI .uniq returns all the values as they are and doesn't remove
duplicates. .uniq! gets rid of all the values and leaves me with no data
at all

No, I know exactly what Array#uniq! does:
------------------------------------------------------------ Array#uniq!
     array.uniq! -> array or nil

···

On Wed, Feb 11, 2009 at 4:51 PM, Stuart Little < stuart_clarke1986@hotmail.co.uk> wrote:
------------------------------------------------------------------------
     Removes duplicate elements from self. Returns nil if no changes
     are made (that is, no duplicates are found).

        a = [ "a", "a", "b", "b", "c" ]
        a.uniq! #=> ["a", "b", "c"]
        b = [ "a", "b", "c" ]
        b.uniq! #=> nil

As I said I wasn't sure what you were trying to accomplish and what object
should eliminate duplicates.

If you think about my example a bit, I suspect you'll be able to suss it
out.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale

Can you show us the contents of the array and the result of #uniq? For example:

  my_array = <some code to fill the array>

  p my_array
  p my_array.uniq

Regards,
Pit

···

2009/2/11 Stuart Little <stuart_clarke1986@hotmail.co.uk>:

I want something simpler than that to be honest. I just want to get rid
of all duplicate entries in the data set and leave just one of each
unique value in my array.

Also, FYI .uniq returns all the values as they are and doesn't remove
duplicates. .uniq! gets rid of all the values and leaves me with no data
at all

Forgive me. What does that line do? The output is listed earlier in the
thread.

Many thanks

Pit Capitain wrote:

···

2009/2/11 Stuart Little <stuart_clarke1986@hotmail.co.uk>:

     time = event.time_written.to_s
     dateCheck = [time.gsub(/\s/, '')[0..7] + time[26..29]]
     printdata = [dateCheck]
     @alerts.push printdata.uniq

Stuart, in this code you have an array with exactly one element, so
#uniq returns the original array. Can you add the line "p printdata"
and show us the output?

Regards,
Pit

--
Posted via http://www.ruby-forum.com/\.

Ok

This

dateCheck = [time.gsub(/\s/, '')[0..7] + time[26..29]]
      #printdata.push(dateCheck)
      @alerts.push dateCheck

gets me:

TueAug052008
TueAug052008
TueAug052008
WedAug062008
WedAug062008
TueAug052008

and this

dateCheck = [time.gsub(/\s/, '')[0..7] + time[26..29]]
      #printdata.push(dateCheck)
      @alerts.push dateCheck.uniq

gets me

TueAug052008
TueAug052008
TueAug052008
WedAug062008
WedAug062008
TueAug052008

Same data for both. When I do uniq! I get nothing

Many thanks

Pit Capitain wrote:

···

2009/2/11 Stuart Little <stuart_clarke1986@hotmail.co.uk>:

I want something simpler than that to be honest. I just want to get rid
of all duplicate entries in the data set and leave just one of each
unique value in my array.

Also, FYI .uniq returns all the values as they are and doesn't remove
duplicates. .uniq! gets rid of all the values and leaves me with no data
at all

Can you show us the contents of the array and the result of #uniq? For
example:

  my_array = <some code to fill the array>

  p my_array
  p my_array.uniq

Regards,
Pit

--
Posted via http://www.ruby-forum.com/\.

That makes sense but how do I avoid this? I cant see how. I need to
dedupe before the data goes into @alerts.

David A. Black wrote:

···

Hi --

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)

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!

--
Posted via http://www.ruby-forum.com/\.

This is not what I suggested. I just wanted the output of

  p dateCheck
  p dateCheck.uniq

and I'm sure we can tell you immediately what's going wrong.

If you don't want to do this for some reason, I repeat what I've
written before: you still have an array with just one element: a
string of size 12. So #uniq returns the same array. But if you still
say that you get six lines of output, I suspect that you execute the
code you've shown in a loop and that you are filling the array
@alerts. If this is the case, you are using #uniq at the wrong place.
Either do @alerts.uniq *after* the loop or use the code David has
shown you.

Regards,
Pit

···

2009/2/11 Stuart Little <stuart_clarke1986@hotmail.co.uk>:

This

dateCheck = [time.gsub(/\s/, '')[0..7] + time[26..29]]
     #printdata.push(dateCheck)
     @alerts.push dateCheck

gets me:

TueAug052008
TueAug052008
TueAug052008
WedAug062008
WedAug062008
TueAug052008

Hi --

David A. Black 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

···

On Thu, 12 Feb 2009, Stuart Little wrote:

On Thu, 12 Feb 2009, Stuart Little wrote:

--
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!

David and I are trying to tell you the same thing.

My last attempt to get through

date = "TueAug052008"
dateCheck =
alerts =
5.times do
  dateCheck.push date
  alerts.push(date) unless alerts.include?(date)
end

puts "at end alerts is #{alerts.inspect}"
puts " and dateCheck is #{dateCheck.inspect}"

at end alerts is ["TueAug052008"]
and dateCheck is ["TueAug052008", "TueAug052008", "TueAug052008",
"TueAug052008", "TueAug052008"]

···

On Wed, Feb 11, 2009 at 5:33 PM, Stuart Little < stuart_clarke1986@hotmail.co.uk> wrote:

That makes sense but how do I avoid this? I cant see how. I need to
dedupe before the data goes into @alerts.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale

Sorry I seemed to have missed your first post. I have tried this code
which is already in a loop so I have

@date =
@dateCheck =
@alerts =
array do |event|

time = event.time_written.to_s
      @date = time.gsub(/\s/, '')[0..7] + time[26..29]
      @dateCheck.push @date
      @alerts.push(@date) unless @alerts.include?(@date)

Inside my loop I have the same problem, outside the loop I get just one
entry which again is not what I need. I also tried before I posted on
the forum putting array.uniq outside of the loop and I just got one
entry again which is incorrect.

Rick Denatale wrote:

···

On Wed, Feb 11, 2009 at 5:33 PM, Stuart Little < > stuart_clarke1986@hotmail.co.uk> wrote:

That makes sense but how do I avoid this? I cant see how. I need to
dedupe before the data goes into @alerts.

David and I are trying to tell you the same thing.

My last attempt to get through

date = "TueAug052008"
dateCheck =
alerts =
5.times do
  dateCheck.push date
  alerts.push(date) unless alerts.include?(date)
end

puts "at end alerts is #{alerts.inspect}"
puts " and dateCheck is #{dateCheck.inspect}"

at end alerts is ["TueAug052008"]
and dateCheck is ["TueAug052008", "TueAug052008", "TueAug052008",
"TueAug052008", "TueAug052008"]

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale

--
Posted via http://www.ruby-forum.com/\.

Up and running now thanks

Rick Denatale wrote:

···

On Wed, Feb 11, 2009 at 5:33 PM, Stuart Little < > stuart_clarke1986@hotmail.co.uk> wrote:

That makes sense but how do I avoid this? I cant see how. I need to
dedupe before the data goes into @alerts.

David and I are trying to tell you the same thing.

My last attempt to get through

date = "TueAug052008"
dateCheck =
alerts =
5.times do
  dateCheck.push date
  alerts.push(date) unless alerts.include?(date)
end

puts "at end alerts is #{alerts.inspect}"
puts " and dateCheck is #{dateCheck.inspect}"

at end alerts is ["TueAug052008"]
and dateCheck is ["TueAug052008", "TueAug052008", "TueAug052008",
"TueAug052008", "TueAug052008"]

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale

--
Posted via http://www.ruby-forum.com/\.