[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array uniq

Stuart Little

2/11/2009 8:52:00 PM

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

27 Answers

David A. Black

2/11/2009 9:03:00 PM

0

On Thu, 12 Feb 2009, Stuart Little 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)

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Rick DeNatale

2/11/2009 9:31:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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.denh...
Twitter: http://twitter.com/Ri...

Stuart Little

2/11/2009 9:41:00 PM

0

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.r...
> Coming in 2009: The Well-Grounded Rubyist (http://manning....)
>
> http://www.wis... => Independent, social wishlist management!

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

Pit Capitain

2/11/2009 9:51:00 PM

0

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

Stuart Little

2/11/2009 9:52:00 PM

0

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

Stuart Little

2/11/2009 9:58:00 PM

0

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

David A. Black

2/11/2009 10:00:00 PM

0

Hi --

On Thu, 12 Feb 2009, Stuart Little wrote:

> 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

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Rick DeNatale

2/11/2009 10:01:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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

> 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
------------------------------------------------------------------------
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.denh...
Twitter: http://twitter.com/Ri...

Pit Capitain

2/11/2009 10:01:00 PM

0

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

Stuart Little

2/11/2009 10:18:00 PM

0

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