我正在尝试迭代动态数组并在超出日期时删除元素。该数组是使用逗号分隔的字符串生成的。
我的代码如下:
Sub delete(ByRef ar, ByVal idx)
Dim i
Dim ub
ub = UBound(ar) - 1
For i = idx To ub
ar(i) = ar(i + 1)
Next
ReDim Preserve ar(ub)
End Sub
Dim invitation,invitation_array
invitation = rs("visningID")
invitation_array=split(invitation,",")
invitationStr = "Select * From visningTable Where Id=" & invitation_array(v) & ""
Set rsInvitation = Conn.Execute(invitationStr)
For v=LBound(invitation_array) to UBound(invitation_array)
If CDate(rsInvitation("Dato")) < Date then
Dim ar
Dim i
ar = invitation_array
delete ar, v '' pass the name of the array, and the index of the element to delete
end if
我无法删除正确的元素,最终数组完全搞砸了? 我哪里出错?
答案 0 :(得分:0)
对数组进行重复数据删除的最佳工具是Dictionary:
>> a = Array( 1, 2, 3, 3, 2, 1 )
>> Set d = CreateObject("Scripting.Dictionary")
>> For Each e In a : d(e) = 0 : Next
>> b = d.Keys()
>> WScript.Echo Join(b), TypeName(b), TypeName(b(0)), b(0)
>>
1 2 3 Variant() Integer 1
(See also)
如果您想要使用唯一元素替换,只需指定a = b
。