Last active
October 4, 2022 12:42
-
-
Save gwobcke/1044367 to your computer and use it in GitHub Desktop.
Classic ASP Check If In 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
<% | |
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 | |
%> |
๐
๐
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
%>
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
Need a Dim i inside of the Function so that you are not rewriting any global variables named i