Cycling Through Arrays to Find Match

# Thread: Cycling Through Arrays to Find Match

1. Member
Join Date
Dec 1969
Posts
69

## Cycling Through Arrays to Find Match

Is there an easier way to cycle through an array to find if a value exists in one of the subsets?<BR><BR>At the moment, I&#039;m stuck with writing it like so:<BR>[code language="ASP"]<BR><BR>For Each itm in Session("Managed_Group")<BR> If Session("Managed_Group")(itm) = itm Then<BR> Response.Write "some link"<BR> End If<BR>Next<BR>[/code]

2. Member
Join Date
Dec 1969
Posts
69

## RE: Cycling Through Arrays to Find Match

Sorry, that last code was totally off:<BR><BR>Let&#039;s see if this makes more sense:<BR><BR>[code language="asp"]<BR>&#039;//-------------------------------------------------------------------------------------------------<BR>&#039;// Searches an array for a specific value. Returns true if the item is found.<BR>&#039;//-------------------------------------------------------------------------------------------------<BR>Function SearchArray(Arr, itm)<BR> For Each obj in Arr<BR> If Arr(obj) = itm Then <BR> SearchArray = true<BR> End If <BR> Next <BR>End Function<BR>[/code]

3. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Cycling Through Arrays to Find Match

No, although i&#039;ll make a suggestion:<BR><BR>Function SearchArray(Arr, itm) <BR> For Each obj in Arr <BR> If Arr(obj) = itm Then <BR> SearchArray = true<BR> Exit Function<BR> End If <BR> Next <BR>End Function <BR><BR>Craig.

4. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## What about Filter()...

..<BR><BR>Couldn&#039;t you do a Filter() on the Array and see if anything was returned?<BR><BR>Alternatively, you could do a Join() to build a string and then use InStr() to see if the item was found.

5. Member
Join Date
Dec 1969
Posts
69

## RE: What about Filter()...

I do not think Filter() would work, as I&#039;ve read up on that and w3schools says it would work on strings only.<BR><BR>I wouldn&#039;t want it to return true when searching for 31, and 310 was in the records (where Filter would return true because &#039;31&#039; is in &#039;310&#039;).<BR><BR>Or am I totally off base? I didn&#039;t mention that this array was supposed to hold integer values in my above posts, sorry.

6. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## Filter() works with arrays.

See the VBScript Reference link in the navigation to the left. It&#039;s under "Functions".<BR><BR>But, I&#039;m not sure about the 31 vs. 310.<BR><BR>You should be able to do the Join() and InStr() suggestion that I gave. Just prepend and append your delimiter and when you do your search, include that delimiter (e.g. [code language="VBScript"]"," & search & ","[/code])

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•