/ Published in: ASP
This function returns the array with all instances of the specified value removed
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
'This function returns a copy of the array with all instances of the specified value removed function removeValueFromArray(byval arrayName, valueToRemove) amountFound = 0 'keeps track of how many it found so that it knows what size to redim the array at the end topIndex = ubound(arrayName) 'hold the value of the ubound in a variable so that you can decrement it when you find the value to remove for i = 0 to ubound(arrayName) if i > topIndex then 'this keeps the loop from checking past the topIndex. keeps the loop from being infinite when the last value exit for ' exit the loop when you have reached the end of the checked values end if if arrayName(i) = valueToRemove then topIndex = topIndex - 1 'decrement the topIndex when you shift down so that it doesn't check the duplicated values at the end of the array amountFound = amountFound + 1 ' the value has been found so increment for j = i to (ubound(arrayName) - 1) 'shift the array values left to overwrite the value to remove arrayName(j) = arrayName(j + 1) next 'if the next element was equal to valueToRemove you have to go back and get rid of it too if arrayName(i) = valueToRemove then i = i - 1 ' this gets incremented at the beginning of the loop so don't worry about negative values end if end if next redim preserve arrayName(ubound(arrayName) - amountFound) removeValueFromArray = arrayName end function