Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding an item in an array

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a large array (about 15500 items) and I am currently searching through it using a for each loop:

for each item in MyArray
if item="what I'm looking for" then
some code here
exit for
end if
next

but the problem is that this is extremely slow, as this bit of code is being repeated about 14000 times for the program I'm making.

I was wondering if there's a quicker way to find if an item exists in an array?
 
squet,

First, I would change your code into a function so that the system does not have to "recompile" your code everytime it does a search like:

Function FindString(Array, string)
For I = 0 to Ubound(Array,I) - 1
For J = 0 to Ubound(Array,J) - 1
If array(J,I) = "what I'm looking for" then
'Do this
Findstring = TRUE
End

Next
Next
End Function

The other thing I would try is if you know your string will be in a certain part of the array most of the time, search that part first.

Cheers
fengshui1998
 
Place the array in a db/tble and use the db functions to lookup the value.

If this is beyond your ability, then do an actual sort of the array and do a binary search of the sorted content. If you are adding/editing/deleting itemens in the array, you will also need to re-sort them each time a change is made. this would generally be done with routines which re-write the array based on the changed element(s), not just an append and re-sort.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Here. This should do it for you. It is a search routine that cuts the array in half each pass. It is very quick. It needs a sorted list however....

'This is driver stuff and just sets up the array
Dim arrTest()
ReDim arrTest(1)
For i = 1 To 200000
arrTest(i - 1) = "Test" & i
ReDim Preserve arrTest(i)
Next
If IsInArray(arrTest, "Test362") Then Response.Write("Found It")

'This is the meat and potatoes
Function IsInArray(SortedArray(), strToFind)
Dim minIndex
Dim maxIndex
Dim centerIndex

minIndex = LBound(SortedArray)
maxIndex = UBound(SortedArray) - 1
centerIndex = Round((maxIndex - minIndex) / 2)

IsInArray = False

Do While maxIndex <> centerIndex
Select Case StringCompare(SortedArray(centerIndex), strToFind)

Case -1
minIndex = centerIndex
centerIndex = Round((maxIndex - minIndex) / 2 + 0.1) + minIndex
Case 0
IsInArray = True
Exit Do
Case 1
maxIndex = centerIndex
centerIndex = Round((maxIndex - minIndex) / 2) + minIndex
End Select
Loop
End Function

Function StringCompare(strIn1, strIn2)
If Len(strIn2) > Len(strIn1) Then
StringCompare = -1
ElseIf Len(strIn2) < Len(strIn1) Then
StringCompare = 1
Else
For i = 1 To Len(strIn1)
If Mid(strIn1, i, 1) < Mid(strIn2, i, 1) Then
StringCompare = -1
Exit For
ElseIf Mid(strIn1, i, 1) > Mid(strIn2, i, 1) Then
StringCompare = 1
Exit For
Else
StringCompare = 0
End If
Next
End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top