1

I'm trying to iterate through a dynamic array and remove elements if the date has been exceeded. The array has been generated from a comma separated string.

My code is as follows:

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

I can't get it to remove the correct elements and in the end the array is completely messed up? Where do I go wrong?

  • Well I guess I know what goes wrong. As the code iterates the changing index numbers of the array messes everything up. – Simon Lytting Feb 12 '15 at 15:47
  • Why not using SQL only to get what you need? `strSQL = "Select Id From visningTable Where Id In (" & invitation_array & ") And Dato>=GetDate()"` will return only records which are both in the array and have date bigger than the current date. Isn't this what you're looking for? – Shadow The GPT Wizard Feb 15 '15 at 13:29

1 Answers1

0

The best tool for de-duplicating an array is the 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)

If you want to replace a with the unique elements, just assign a = b.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96