Skip to content

Instantly share code, notes, and snippets.

@gwobcke
Last active October 4, 2022 12:42
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save gwobcke/1044367 to your computer and use it in GitHub Desktop.
Save gwobcke/1044367 to your computer and use it in GitHub Desktop.
Classic ASP Check If In Array
<%
Function in_array(element, arr, performTrim)
Dim i
in_array = False
For i=0 To Ubound(arr)
If performTrim Then '//there are some scenarios where you want to trim
If Trim(arr(i)) = Trim(element) Then
in_array = True
Exit Function
End If
Else '//and other scenarios where you don't
If arr(i) = element Then
in_array = True
Exit Function
End If
End If
Next
End Function
DIM findThis
findThis = "Apple"
fruits = Array("Banana","Apple","Orange")
If in_array(findThis, fruits, false) Then
Response.Write findThis & " is in the array"
Else
Response.Write findThis & " is not in the array"
End If
%>
@jonhargett
Copy link

Need a Dim i inside of the Function so that you are not rewriting any global variables named i

Copy link

ghost commented Apr 26, 2016

👍

@alancoleman
Copy link

👍

@tiagojcperez
Copy link

Upgrade: Needle can be a String or an Array.
Add the performTrim param if you want to.

<%
Function InArray(Needle, Haystack)
    Dim i, x
    InArray = False

    For i = 0 To Ubound(Haystack)
        If IsArray(Needle) = True Then
            For x = 0 To Ubound(Needle)
                If Trim(Haystack(i)) = Trim(Needle(x)) Then
                    InArray = True
                    Exit Function
                End If
            Next
        Else
            If Trim(Haystack(i)) = Trim(Needle) Then
                InArray = True
                Exit Function
            End If
        End If
    Next
End Function
%>

@sergioruizdeveloper
Copy link

Thank you, It is very useful for my work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment