By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,704 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,704 IT Pros & Developers. It's quick & easy.

Checking for duplication in an array or delimited string

P: n/a
CJM
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

--
cj*******@REMOVEMEyahoo.co.uk
[remove the obvious bits]
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"CJM" <cj*******@newsgroup.nospam> wrote in message
news:Ow*************@TK2MSFTNGP14.phx.gbl...
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

--
cj*******@REMOVEMEyahoo.co.uk
[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
Jul 22 '05 #2

P: n/a
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[i] & "; ") = 0 then
strCheck = strCheck & myarray[i] & "; "
else
' duplicate serial
end if
next

of course with large arrays this is not performant at all... :-)
"CJM" <cj*******@newsgroup.nospam> wrote in message
news:Ow*************@TK2MSFTNGP14.phx.gbl...
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

--
cj*******@REMOVEMEyahoo.co.uk
[remove the obvious bits]

Jul 22 '05 #3

P: n/a
CJM

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:1b********************@comcast.com...


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
Jul 22 '05 #4

P: n/a
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" <cj*******@newsgroup.nospam> wrote in message
news:Oj**************@TK2MSFTNGP15.phx.gbl...

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:1b********************@comcast.com...


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

Jul 22 '05 #5

P: n/a
CJM

"Mark Schupp" <no******@email.net> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.