Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Determining DISTINCT strings from a list

Reply
Thread Tools

Determining DISTINCT strings from a list

 
 
CJM
Guest
Posts: n/a
 
      01-23-2004
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


 
Reply With Quote
 
 
 
 
William Morris
Guest
Posts: n/a
 
      01-23-2004
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.

--
William Morris
Product Development, Seritas LLC
Kansas City, Missouri

"CJM" <(E-Mail Removed)> wrote in message
news:%237c3h%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
CJM
Guest
Posts: n/a
 
      01-23-2004
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


"William Morris" <(E-Mail Removed)> wrote in
message news:burcr9$ka333$(E-Mail Removed)-berlin.de...
> 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.
>
> --
> William Morris
> Product Development, Seritas LLC
> Kansas City, Missouri
>
> "CJM" <(E-Mail Removed)> wrote in message
> news:%237c3h%(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
William Morris
Guest
Posts: n/a
 
      01-23-2004
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)





"CJM" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "William Morris" <(E-Mail Removed)> wrote in
> message news:burcr9$ka333$(E-Mail Removed)-berlin.de...
> > 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.
> >
> > --
> > William Morris
> > Product Development, Seritas LLC
> > Kansas City, Missouri
> >
> > "CJM" <(E-Mail Removed)> wrote in message
> > news:%237c3h%(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Ray at
Guest
Posts: n/a
 
      01-23-2004
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


"CJM" <(E-Mail Removed)> wrote in message
news:%237c3h%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Aaron Bertrand - MVP
Guest
Posts: n/a
 
      01-23-2004
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/




"CJM" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "William Morris" <(E-Mail Removed)> wrote in
> message news:burcr9$ka333$(E-Mail Removed)-berlin.de...
> > 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.
> >
> > --
> > William Morris
> > Product Development, Seritas LLC
> > Kansas City, Missouri
> >
> > "CJM" <(E-Mail Removed)> wrote in message
> > news:%237c3h%(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Chris Hohmann
Guest
Posts: n/a
 
      01-24-2004
"Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
news:(E-Mail Removed)...
> "CJM" <(E-Mail Removed)> wrote in message
> news:%237c3h%(E-Mail Removed)...
> > 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
> >
> >

> 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


 
Reply With Quote
 
Yan-Hong Huang[MSFT]
Guest
Posts: n/a
 
      01-26-2004
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.

 
Reply With Quote
 
CJM
Guest
Posts: n/a
 
      01-26-2004
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

"CJM" <(E-Mail Removed)> wrote in message
news:%237c3h%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
CJM
Guest
Posts: n/a
 
      01-26-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
Distinct list based off values of two elements johkar XML 2 12-04-2009 04:45 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Determining the length of strings in a list robin.siebler@palmsource.com Python 2 03-14-2005 08:29 PM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM



Advertisments