Skip to content

Instantly share code, notes, and snippets.

@sholsinger
Created April 26, 2011 20:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sholsinger/943116 to your computer and use it in GitHub Desktop.
Save sholsinger/943116 to your computer and use it in GitHub Desktop.
VBScript tool for checking if a value is in an array.
' 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
@buzibu
Copy link

buzibu commented Jul 18, 2014

I had to add
Dim i
in order to have this code running, otherwise it gave me an error "Variable i is undefined"

@an9elia
Copy link

an9elia commented Jun 30, 2016

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

@strider72
Copy link

strider72 commented Jan 10, 2017

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

@melaku-z
Copy link

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