Determining DISTINCT strings from a list

Discussion in 'ASP General' started by CJM, Jan 23, 2004.

  1. CJM

    CJM Guest

    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
     
    CJM, Jan 23, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:%237c3h%...
    > 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
    >
    >
     
    William Morris, Jan 23, 2004
    #2
    1. Advertising

  3. CJM

    CJM Guest

    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" <> wrote in
    message news:burcr9$ka333$-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" <> wrote in message
    > news:%237c3h%...
    > > 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
    > >
    > >

    >
    >
     
    CJM, Jan 23, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > 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" <> wrote in
    > message news:burcr9$ka333$-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" <> wrote in message
    > > news:%237c3h%...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    William Morris, Jan 23, 2004
    #4
  5. CJM

    Ray at Guest

    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" <> wrote in message
    news:%237c3h%...
    > 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
    >
    >
     
    Ray at, Jan 23, 2004
    #5
  6. 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" <> wrote in message
    news:...
    > 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" <> wrote in
    > message news:burcr9$ka333$-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" <> wrote in message
    > > news:%237c3h%...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    Aaron Bertrand - MVP, Jan 23, 2004
    #6
  7. "Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
    news:...
    > "CJM" <> wrote in message
    > news:%237c3h%...
    > > 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
     
    Chris Hohmann, Jan 24, 2004
    #7
  8. 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.
     
    Yan-Hong Huang[MSFT], Jan 26, 2004
    #8
  9. CJM

    CJM Guest

    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" <> wrote in message
    news:%237c3h%...
    > 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
    >
    >
     
    CJM, Jan 26, 2004
    #9
  10. CJM

    CJM Guest

    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
     
    CJM, Jan 26, 2004
    #10
  11. 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.
     
    Yan-Hong Huang[MSFT], Jan 27, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Klaus Neuner
    Replies:
    7
    Views:
    527
    Klaus Neuner
    Jul 26, 2004
  2. Replies:
    2
    Views:
    322
    Michael Spencer
    Mar 14, 2005
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  4. johkar
    Replies:
    2
    Views:
    1,394
    johkar
    Dec 4, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    420
    Kai-Uwe Bux
    Apr 11, 2010
Loading...

Share This Page