
is it in the array, problems
I copied some code from the aspfaqs site <BR>********************************<BR>Function ElementInArray(aMyArray, strLookingFor, compare)<BR>'Returns True if strLookingFor is in aMyArray, False otherwise<BR> ElementInArray = (UBound(Filter(aMyArray, strLookingFor, True, compare)) > 1)<BR>End Function<BR>************************************** **<BR>And I wanted to use it to see if my value (let say 7) was in the array (let say the array is "1, 2, 3, 4, 5, 6, 9, 11, 12, 13, 15, 17, 20) so I run the function and it says True because its sees 17 and that qualifies as 7 being in the array but not in my book. Should I write a big loop that does "if value is array(X) then "and all that stuff?<BR>I am still kind of new to arrays, so any help would be greatly appreciated.<BR>

RE: is it in the array, problems
It depends what your situation is...<BR>If you're after performance, (and your array is always in order) then you should write yourself a binary search function.<BR><BR>If you're after simple code, what's more simple than a loop:<BR>For i = 0 to ubound(aMyArray)<BR> if aMyArray(i) = value Then<BR> blnFound = True<BR> Exit For<BR> end if<BR>Next i<BR><BR>Make Sense?<BR><BR>Jason

RE: is it in the array, problems
yes it makes sense, but it suffers from the same exact problem as the one i am using. My data will always be in order, what is the binary serch you speak of?

ah HA! I got it, Maybe not.
The problem is that its looking at strings 7 is in the array because 17 is... so if I add a little line of code that adds a space to the beginning of the searchstinrg making "7" into " 7" it works perfectly because the array looks like this 1, 2, 3, 17, ... and since there is not a space inbetween 1 and 7, 17 does not equal " 7" but I just realized something(while typing this). This really does not work cause if the array is 2, 3, 11 and the search string is 1 its will still find it because 11 is in there. ****, ok next idea, what about making it look for numbers instead of stings. I tried replacing Cstr with clng and it freaked. any ideas?

RE: ah HA! I got it, Maybe not.
duh ! jsut do the same thing turn "7" into " 7," so even " 770" will not be it. still would like to know what this binary search is....

RE: ah HA! I got it, Maybe not.
Binary search isn't worth the trouble unless you've got lots of elements in the array.<BR><BR>What is lots? Depends.<BR><BR>With VBScript I'd guess at least 30 or so. <BR><BR>The principle is simple, same as what you would do if somebody asked you to guess a number from 1 to 100:<BR><BR>Is it greater than 50? Yes.<BR>Is it greater than 75? No.<BR>Is it greater than 62? No.<BR>Is it greater than 56? Yes.<BR>Is it greater than 59? Yes.<BR>Is it greater than 60? It is equal to 60.<BR><BR>DO the same thing with the array: Look at the middle element. Is it more or less than your test value? If more, then look at the upper half of the array, only. In that upper half, look at the middle element... etc., etc.<BR><BR>

last resort, I'm getting tired and upset.
is there anyway to make this work correctly instead of seeing everything as strings(which really doenst work that well)? can i make it see then item in the array as numbers? I would like to keep the numbers as they are the ID of items in another table. Any other suggestions welcome.

RE: last resort, I'm getting tired and upset.
Any Ideas on how to make this work better, other than the binary searchthing.

Why did you use strings, then???
You can put anything you want into an array...numbers, strings, even other arrays.<BR><BR>So why did you put strings into that array if you didn't want to compare strings????<BR><BR>Anyway, sure:<BR><BR><%<BR>findThis = CDbl(findThis) ' ensure test value is numeric<BR>foundAt = 1<BR><BR>For i = 0 To UBound(ary)<BR> If CDBL(ary(i)) = findThis Then<BR> foundAt = i<BR> Exit For<BR> End If<BR>Next<BR>If foundAt >= 0 Then ....<BR><BR>%><BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

