Created
April 26, 2011 20:52
-
-
Save sholsinger/943116 to your computer and use it in GitHub Desktop.
VBScript tool for checking if a value is in an array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' returns the index of obj in array. obj can be anything. Returns -1 if not found. | |
Function inArray(arr, obj) | |
On Error Resume Next | |
Dim x: x = -1 | |
If isArray(arr) Then | |
For i = 0 To UBound(arr) | |
If arr(i) = obj Then | |
x = i | |
Exit For | |
End If | |
Next | |
End If | |
Err.Clear() | |
inArray = x | |
End Function |
Function inArray(iArray, value)
Dim found: found = False
For i = 0 To UBound(iArray)
If iArray(i) = value Then
found = True
Exit For ' prevents searching the whole array...
End If
Next
inArray = found
End Function
public function in_array(needle, haystack)
in_array = False
needle = trim(needle)
For Each hay in haystack
If trim(hay) = needle Then
in_array = True
Exit For
End If
Next
end function
Function inArray(arr, obj)
inArray = False
For Each value in arr
If value = obj Then
inArray = True
Exit For
End If
Next
End Function
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to add
Dim i
in order to have this code running, otherwise it gave me an error "Variable i is undefined"