I need to de-dup an array I currently define as
DIM ISBN(500). I found nice de-dup code to use, but I'm getting a "type mismatch" error. I put together a simple test case that shows a working and non-working approach, but I don't understand why the explicit Dim won't work or how to actually use the MyArray=Array( ...) if I have to initialize a large array to establish its size or if I have to redo my code to use dynamic arrays (with which I'm less familiar.)
Somebody please clear up my ignorance. Thanks. Here's code:
THIS VERSION GETS "TYPE MISMATCH" ERROR
----------------------------------------
<%@Language=VBscript%>
<%Option Explicit%>
<html>
<head>
<title>TempTest</title>
</head>
<body>
<%
Dim MyList, MyArray(10), arrString, I
'--- Initialize full array
For I = 0 to UBound(MyArray)
MyArray(I)=""
next
'--- add some test values
MyArray(0)="dog"
MyArray(1)="cat"
MyArray(2)="deer"
MyArray(3)="dog"
MyArray(4)="lion"
MyArray(5)="dog"
for I = 0 to UBound(MyArray)
response.write "before: "& MyArray(I) & "<BR>"
next
'--- remove duplicates
MyArray=RemDups(MyArray)
for I = 0 to UBound(MyArray)
response.write "after: " & MyArray(I) & "<BR>"
next
' remove duplicates from ISBN array
' original source code from Function RemDups(ByVal anArray)
Dim d, item, thekeys
Set d = CreateObject("Scripting.Dictionary"
d.removeall
d.CompareMode = 0
For Each item In anArray
If Not d.Exists(item) Then d.Add item, item
Next
thekeys = d.keys
Set d = Nothing
RemDups = thekeys
End Function
%>
</body>
</html>
------------------------------
THIS VERSION WORKS
------------------------------
<%@Language=VBscript%>
<%Option Explicit%>
<html>
<head>
<title>TempTest</title>
</head>
<body>
<%
Dim MyList, MyArray, arrString, I
'--- Initialize full array
MyArray=Array("","","","","","","","","",""
'--- add some test values
MyArray(0)="dog"
MyArray(1)="cat"
MyArray(2)="deer"
MyArray(3)="dog"
MyArray(4)="lion"
MyArray(5)="dog"
for I = 0 to UBound(MyArray)
response.write "before: "& MyArray(I) & "<BR>"
next
'--- remove duplicates
MyArray=RemDups(MyArray)
for I = 0 to UBound(MyArray)
response.write "after: " & MyArray(I) & "<BR>"
next
' remove duplicates from ISBN array
' original source code from Function RemDups(ByVal anArray)
Dim d, item, thekeys
Set d = CreateObject("Scripting.Dictionary"
d.removeall
d.CompareMode = 0
For Each item In anArray
If Not d.Exists(item) Then d.Add item, item
Next
thekeys = d.keys
Set d = Nothing
RemDups = thekeys
End Function
%>
</body>
</html>
DIM ISBN(500). I found nice de-dup code to use, but I'm getting a "type mismatch" error. I put together a simple test case that shows a working and non-working approach, but I don't understand why the explicit Dim won't work or how to actually use the MyArray=Array( ...) if I have to initialize a large array to establish its size or if I have to redo my code to use dynamic arrays (with which I'm less familiar.)
Somebody please clear up my ignorance. Thanks. Here's code:
THIS VERSION GETS "TYPE MISMATCH" ERROR
----------------------------------------
<%@Language=VBscript%>
<%Option Explicit%>
<html>
<head>
<title>TempTest</title>
</head>
<body>
<%
Dim MyList, MyArray(10), arrString, I
'--- Initialize full array
For I = 0 to UBound(MyArray)
MyArray(I)=""
next
'--- add some test values
MyArray(0)="dog"
MyArray(1)="cat"
MyArray(2)="deer"
MyArray(3)="dog"
MyArray(4)="lion"
MyArray(5)="dog"
for I = 0 to UBound(MyArray)
response.write "before: "& MyArray(I) & "<BR>"
next
'--- remove duplicates
MyArray=RemDups(MyArray)
for I = 0 to UBound(MyArray)
response.write "after: " & MyArray(I) & "<BR>"
next
' remove duplicates from ISBN array
' original source code from Function RemDups(ByVal anArray)
Dim d, item, thekeys
Set d = CreateObject("Scripting.Dictionary"
d.removeall
d.CompareMode = 0
For Each item In anArray
If Not d.Exists(item) Then d.Add item, item
Next
thekeys = d.keys
Set d = Nothing
RemDups = thekeys
End Function
%>
</body>
</html>
------------------------------
THIS VERSION WORKS
------------------------------
<%@Language=VBscript%>
<%Option Explicit%>
<html>
<head>
<title>TempTest</title>
</head>
<body>
<%
Dim MyList, MyArray, arrString, I
'--- Initialize full array
MyArray=Array("","","","","","","","","",""
'--- add some test values
MyArray(0)="dog"
MyArray(1)="cat"
MyArray(2)="deer"
MyArray(3)="dog"
MyArray(4)="lion"
MyArray(5)="dog"
for I = 0 to UBound(MyArray)
response.write "before: "& MyArray(I) & "<BR>"
next
'--- remove duplicates
MyArray=RemDups(MyArray)
for I = 0 to UBound(MyArray)
response.write "after: " & MyArray(I) & "<BR>"
next
' remove duplicates from ISBN array
' original source code from Function RemDups(ByVal anArray)
Dim d, item, thekeys
Set d = CreateObject("Scripting.Dictionary"
d.removeall
d.CompareMode = 0
For Each item In anArray
If Not d.Exists(item) Then d.Add item, item
Next
thekeys = d.keys
Set d = Nothing
RemDups = thekeys
End Function
%>
</body>
</html>