Determining DISTINCT strings from a list

C

CJM

I have a bit of code which involves some looping...

In each iteration, we retrieve a string value... I want to build a list of
DISTINCT (in the SQL sense) string values.

Our example strings might be 'ABCD', 'ABCDXX', 'ABCDYY', 'ABCDZZ', 'ABCD',
'ABCDYY'
In which case, we want to end up with 'ABCD', 'ABCDXX', 'ABCDYY', 'ABCDZZ'

It's as simple as that.... I'm not bothered what structures we use...


It is easy to build a solution using loads of nested loops, but it seems a
bit inefficient, so I'm wanting to see if there some good suggestions from
the floor...

Thanks in advance

Chris
 
W

William Morris

Split the string into an array and sort it. Then, loop the array, caching
each value and throwing each new one found into a new string.

My .02.
 
C

CJM

In simple english, it seems straightforward, but if you tried to code it I
think you will hit the same problem.

So you iterate through the loop,buikding up an array of all values..

You say to then loop the array, spotting 'each new one', which you then
place elsewhere (maybe in a new array perhaps).

But how exactly do you determine if each value is already there, or if it is
indeed new?

CJM
 
W

William Morris

The key is to sort the array so that like values are together. Then you
code it something like this (written for speed, not accuracy or syntax):

testValue = ""
newList = ""
for counter = 0 to ubound(array)
if testValue <> array(counter) then
testValue = array(counter)
newList = newList & array(counter) & ","
end if
next

response.write left(newList, len(newList) - 1)
 
R

Ray at

You can add your items to a dictionary object to filter out duplicates by
assigning them as keys. Since you cannot have duplicate keys, you can use
an On Error Resume Next to skip the errors (duplicates) and be left with
uniques.

<%
theStrings = Array("ABCDXX", "ABCDYY", "ABCDZZ", "ABCD", "ABCDYY")

Set oDict = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(theStrings)
On Error Resume Next
oDict.Add theStrings(i), theStrings(i)
On Error Goto 0
Next

theFilteredStrings = oDict.Keys()
Set oDicts = Nothing

Response.write "The Filtered list:<br>"
For i = 0 To UBound(theFilteredStrings)
Response.Write theFilteredStrings(i) & "<br>"
Next
%>


Ray at work
 
A

Aaron Bertrand - MVP

Assuming an array that is already sorted, you can monitor repeats by keeping
track of the previous string. If it's the same, it's a double.


<%
currentString = ""
x = array("a", "b", "b", "c")
for i = 0 to ubound(x)
s = x(i)
if i = 0 then
newString = s
currentString = s
else
if s <> currentString then
currentString = s
newString = newString & "," & s
else
' skip
end if
end if
next
Response.Write newString
%>



--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
 
C

Chris Hohmann

Ray at said:
list
You can add your items to a dictionary object to filter out duplicates by
assigning them as keys. Since you cannot have duplicate keys, you can use
an On Error Resume Next to skip the errors (duplicates) and be left with
uniques.

<%
theStrings = Array("ABCDXX", "ABCDYY", "ABCDZZ", "ABCD", "ABCDYY")

Set oDict = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(theStrings)
On Error Resume Next
oDict.Add theStrings(i), theStrings(i)
On Error Goto 0
Next

theFilteredStrings = oDict.Keys()
Set oDicts = Nothing

Response.write "The Filtered list:<br>"
For i = 0 To UBound(theFilteredStrings)
Response.Write theFilteredStrings(i) & "<br>"
Next
%>


Ray at work

To add to Ray's solution, you can take advantage of the fact that
assignment to the Dictionary.Item(key) generates a new key if the key
does not already exist. As such:

<%
Const str = "'ABCD', 'ABCDXX', 'ABCDYY', 'ABCDZZ', 'ABCD','ABCDYY'"
Dim dct,arr,i,iMax
Set dct = CreateObject("Scripting.Dictionary")
arr = Split(str,",")
iMax = UBound(arr,1)
For i = 0 To iMax
dct.Item(Trim(arr(i))) = "Chris is great"
Next
Response.Write Join(dct.Keys,",")
Set dct = Nothing
%>

-Chris Hohmann
 
Y

Yan-Hong Huang[MSFT]

Hello Chris,

Thanks for posting in the community.

Based on my understanding, now the question is: How to remove duplicated
strings in a string list? Please correct me if I have misunderstood the
problem.

I can't think of any better way till now. In ASP.NET, we can make use of
some collection class librarys to see if one string exists in the
collection already. For an example, ArrayList.Contains can be used to
determine whether an element is in the ArrayList or not. I will discuss
with our ASP experts to see whether there is any easy to achieve it and
repy here as soon as possible.

If there is any more questions, please feel free to post here.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

CJM

Thanks to you all for your replies...

I'm afraid for some reason I cant see Ray's post, but fortunately his
solution is tagged on to the end of Chris's post!

I tried a similar thing with just a simple Collection, but it wasnt quite up
to the job. My next step was to have a collection of a UDT which, though a
slightly long-winded, would do the job.

However, I like Ray/Chris's solutions so I'll try them first.


Thanks to all...


Chris
 
C

CJM

I've just looked into the Dictionary solution... I've never used or come
across the object before, but looking at it, it is an obvious solution! It's
effectively what I was hinting at with my proposed Collection/UDT solution.

Cheers Ray!

Chris
 
Y

Yan-Hong Huang[MSFT]

Hi All,

Ray and Chris's idea is creative. I didn't think of it before. :) What I
thought is to do this in compiled code (COM object, ActiveX control) to
improve performance and use it in the page.

Thanks very much for sharing it in the community.

Also, Chris (CJM), thanks for participating the community.

Have a good day.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top