Checking for duplication in an array or delimited string

Discussion in 'ASP General' started by CJM, Mar 21, 2005.

  1. CJM

    CJM Guest

    In an application I'm working the user has the opportunity to record the
    despatching of one or more items with serial numbers.

    For each item they despatch, they have to chose the serial no that they want
    to despatch from a list of available ones. In many cases, these items will
    be of the same time, so the dropdown of available serial no's may be the
    same.

    So now I need to check that the user hasnt picked the same serial no for two
    items....

    The serial no fields all have the same name, so when doing the server side
    validation I'll be presented with a delimited string as a result. What is
    the best way to make sure that none of these serial numbers are duplicated.
    I havent thought of a specific solution yet, but I imagine you could use
    InStr or Arrays to bully the result out, or perhaps enter the values into a
    recordset and manipulate them that way; I havent got as far as figuring out
    any kind of sultion yet, but I figured that someone on the NG will have
    already thought of a quick & efficient approach - I reckon this must be a
    reasonably common issue out there...

    Thanks in advance

    Cheers

    --

    [remove the obvious bits]
     
    CJM, Mar 21, 2005
    #1
    1. Advertising

  2. CJM

    McKirahan Guest

    "CJM" <> wrote in message
    news:...
    > In an application I'm working the user has the opportunity to record the
    > despatching of one or more items with serial numbers.
    >
    > For each item they despatch, they have to chose the serial no that they

    want
    > to despatch from a list of available ones. In many cases, these items will
    > be of the same time, so the dropdown of available serial no's may be the
    > same.
    >
    > So now I need to check that the user hasnt picked the same serial no for

    two
    > items....
    >
    > The serial no fields all have the same name, so when doing the server side
    > validation I'll be presented with a delimited string as a result. What is
    > the best way to make sure that none of these serial numbers are

    duplicated.
    > I havent thought of a specific solution yet, but I imagine you could use
    > InStr or Arrays to bully the result out, or perhaps enter the values into

    a
    > recordset and manipulate them that way; I havent got as far as figuring

    out
    > any kind of sultion yet, but I figured that someone on the NG will have
    > already thought of a quick & efficient approach - I reckon this must be a
    > reasonably common issue out there...
    >
    > Thanks in advance
    >
    > Cheers
    >
    > --
    >
    > [remove the obvious bits]



    The Dictionary object will work as each key must be unique.

    Dim objDIC
    Set objDIC = Server.CreateObject("Scripting.Dictionary")


    Test to see if the serial number exists and, if not, add a serial number to
    the Dictionary.

    If Not objDic.Exists(serialnumber) The
    objDIC.Add serialnumber, ""
    Else
    '* duplicate serialnumber processing
    End If


    Finally, when you're through, do some clean up:

    Set objDIC = Nothing
     
    McKirahan, Mar 21, 2005
    #2
    1. Advertising

  3. CJM

    Thomas Guest

    split the delimited string to an array.

    then either use the dictionary like mckirahan suggested, or just do this:

    dim i, strCheck

    strCheck = " "
    for i = 0 to ubound(myarray) - 1
    if instr(strCheck, " " & myarray & "; ") = 0 then
    strCheck = strCheck & myarray & "; "
    else
    ' duplicate serial
    end if
    next

    of course with large arrays this is not performant at all... :)


    "CJM" <> wrote in message
    news:...
    > In an application I'm working the user has the opportunity to record the
    > despatching of one or more items with serial numbers.
    >
    > For each item they despatch, they have to chose the serial no that they
    > want to despatch from a list of available ones. In many cases, these items
    > will be of the same time, so the dropdown of available serial no's may be
    > the same.
    >
    > So now I need to check that the user hasnt picked the same serial no for
    > two items....
    >
    > The serial no fields all have the same name, so when doing the server side
    > validation I'll be presented with a delimited string as a result. What is
    > the best way to make sure that none of these serial numbers are
    > duplicated. I havent thought of a specific solution yet, but I imagine you
    > could use InStr or Arrays to bully the result out, or perhaps enter the
    > values into a recordset and manipulate them that way; I havent got as far
    > as figuring out any kind of sultion yet, but I figured that someone on the
    > NG will have already thought of a quick & efficient approach - I reckon
    > this must be a reasonably common issue out there...
    >
    > Thanks in advance
    >
    > Cheers
    >
    > --
    >
    > [remove the obvious bits]
    >
     
    Thomas, Mar 21, 2005
    #3
  4. CJM

    CJM Guest

    "McKirahan" <> wrote in message
    news:...
    >
    >
    > The Dictionary object will work as each key must be unique.
    >
    > Dim objDIC
    > Set objDIC = Server.CreateObject("Scripting.Dictionary")
    >
    >
    > Test to see if the serial number exists and, if not, add a serial number
    > to
    > the Dictionary.
    >
    > If Not objDic.Exists(serialnumber) The
    > objDIC.Add serialnumber, ""
    > Else
    > '* duplicate serialnumber processing
    > End If
    >
    >


    I did actually look at the dictionary object, but never came to a
    conclusion, but this solution looks to be both efficient and effective...

    Thanks
     
    CJM, Mar 23, 2005
    #4
  5. CJM

    Mark Schupp Guest

    There are many ways that you could check for duplication each has its pros
    and cons and the performance for each will vary with the number and type of
    the data to be checked.

    One is the dictionary object as mentioned.

    If the list is very short then a "brute force" search (also already
    mentioned) may have better performance than dictionary because there would
    be no overhead for the dictionary object creation.

    For longer lists you could build an array in sorted order and do a binary
    search for duplicate values (look for "binary search" and "insertion sort"
    algorithms).

    If the key values are numeric and always in a restricted range then you
    could also build an array where the key value or a simple derivative of the
    key is the index. This approach would be neater with JScript because JScript
    arrays are dynamic and sparse (only use memory for elements that have
    values).

    You could conceivably build a script object that would pick the "best"
    method based on the number of elements in your original list.

    --
    --Mark Schupp
    Head of Development
    Integrity eLearning
    www.ielearning.com


    "CJM" <> wrote in message
    news:...
    >
    > "McKirahan" <> wrote in message
    > news:...
    >>
    >>
    >> The Dictionary object will work as each key must be unique.
    >>
    >> Dim objDIC
    >> Set objDIC = Server.CreateObject("Scripting.Dictionary")
    >>
    >>
    >> Test to see if the serial number exists and, if not, add a serial number
    >> to
    >> the Dictionary.
    >>
    >> If Not objDic.Exists(serialnumber) The
    >> objDIC.Add serialnumber, ""
    >> Else
    >> '* duplicate serialnumber processing
    >> End If
    >>
    >>

    >
    > I did actually look at the dictionary object, but never came to a
    > conclusion, but this solution looks to be both efficient and effective...
    >
    > Thanks
    >
     
    Mark Schupp, Mar 23, 2005
    #5
  6. CJM

    CJM Guest

    "Mark Schupp" <> wrote in message
    news:...
    > There are many ways that you could check for duplication each has its pros
    > and cons and the performance for each will vary with the number and type
    > of the data to be checked.
    >
    > One is the dictionary object as mentioned.
    >
    > If the list is very short then a "brute force" search (also already
    > mentioned) may have better performance than dictionary because there would
    > be no overhead for the dictionary object creation.
    >
    > For longer lists you could build an array in sorted order and do a binary
    > search for duplicate values (look for "binary search" and "insertion sort"
    > algorithms).
    >
    > If the key values are numeric and always in a restricted range then you
    > could also build an array where the key value or a simple derivative of
    > the key is the index. This approach would be neater with JScript because
    > JScript arrays are dynamic and sparse (only use memory for elements that
    > have values).
    >
    > You could conceivably build a script object that would pick the "best"
    > method based on the number of elements in your original list.
    >
    > --
    > --Mark Schupp
    > Head of Development
    > Integrity eLearning
    > www.ielearning.com
    >


    mark,

    I figure that in the majority of cases the brute force approach would
    technically be the quickest since we are dealing with v. small lists here.
    However, I've opted for the dictionary approach, because a) the overhead is
    not significant enough to affect this low-ish volume system, and b) I like
    it...

    Cheers

    Chris
     
    CJM, Mar 24, 2005
    #6
    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. ll

    duplication?

    ll, Nov 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    402
    Rocky Moore
    Nov 9, 2003
  2. msnews.microsoft.com

    Duplication of html generated from htmlwebresponse

    msnews.microsoft.com, Mar 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    386
    msnews.microsoft.com
    Apr 5, 2005
  3. gh

    storing delimited string in an array

    gh, May 11, 2006, in forum: C Programming
    Replies:
    2
    Views:
    282
    CBFalconer
    May 11, 2006
  4. RyanL
    Replies:
    6
    Views:
    691
    Paul McGuire
    Aug 28, 2007
  5. Sébastien Maurette
    Replies:
    3
    Views:
    143
    David Vallner
    Oct 2, 2006
Loading...

Share This Page