VBScript has a infamously weak implementation of array capabilities. There often comes situations where you’d like to manipulate an array and VBScript just doesn’t have the functionality built in natively.
One of the things that I need to do frequently is delete a value from an array. Well to do this you have too loop through the array and rebuild it as a new array. Since this is such a common task I wrote a little function to do it for me.
Other times I’d simply like to know if an array has a certain value within it. So I can add or remove it or simply act upon it. Well I wrote another one for that too called InArray. It will return a boolean letting you know if the string you are checking for exists within the array.
I will probably add to this later, if anyone has suggestions for additions to this please feel free to forward or submit your code. I’ll almost certainly add sorting code to this later, even though there is a dictionary object available to do that now from VBScript natively.
Function DeleteFromArray(aArray, vElementNumber) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com ' This function will delete the specified element from ' the array passed to the function. For example: ' Array(0) = a ' Array(1) = b ' Array(2) = c ' Array(3) = d ' ' To delete the 'b' from this array you'd simply call ' the function like this: ' Array = DeleteFromArray(Array, 1) Dim aNewArray ReDim aNewArray(0) Dim vLoopValue, vLimit vLoopValue = 0 For Each sValue In aArray If vLoopValue <> vElementNumber Then If vLoopValue = 0 And vElementNumber <> 0 Then aNewArray(vLoopValue) = sValue ElseIf vLoopValue = 1 And vElementNumber = 0 Then aNewArray(vLoopValue) = sValue Else vLimit = Ubound(aNewArray) + 1 ReDim Preserve aNewArray(vLimit) aNewArray(Ubound(aNewArray)) = sValue End If End If vLoopValue = vLoopValue + 1 Next DeleteFromArray = aNewArray End Function Function InArray(aArrayName, sStringToTest) 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com ' This function loops the array and returns a boolean ' depending on wether or not a string exists as one ' of the values within the array. For Example: ' ' Array(0) = "blue" ' Array(1) = "black" ' Array(2) = "red" ' ' InArray(Array, "blue") would return True ' InArray(Array, "green") would return False If IsArray(aArrayName) Then For Each sArrayStringValue In aArrayName sArrayStringValue = sArrayStringValue & "" sStringToTest = sStringToTest & "" If sStringToTest = sArrayStringValue Then InArray = True Exit Function End If Next End If InArray = False End Function