W
William Morris
I have a one-dimesional array of potentially thousands of items, each of
which is a string of at least one up to 30 characters, mixed letters and
numbers. I have another array - potentially as large - whose items I wish
to find in the first array.
A nested loop would answer this problem - and does for the time being.
for x = 0 to ubound(secondarray)
for y = 0 to ubound(firstarray)
if secondarray(x) = firstarray(y) then
itemExists = true
exit function
end if
next
next
I thought it would be better to loop the array and pass each element to a
recursive function that divides and conquers, BUT it misses about half of
the matches - every item I'm searching for already exists in the list, just
as a test. Below is the code I'm using...can anyone figure out what I'm
missing?
Thanks!
- Wm
Function ValueExists(inArray, idToCheck, lowerBound, upperBound)
If lowerBound = upperBound Then
If idToCheck = inArray(lowerBound) Then
ValueExists = True
End If
Exit Function
End If
Dim sPos
sPos = ((upperBound - lowerBound) \ 2) + lowerBound
idToCheck = Trim(idToCheck)
inArray(sPos) = Trim(inArray(sPos))
If idToCheck = inArray(sPos) Then
ValueExists = True
Exit Function
End If
If idToCheck < inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, lowerBound, sPos - 1)
Exit Function
End If
If idToCheck > inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, sPos + 1, upperBound)
Exit Function
End If
End Function
which is a string of at least one up to 30 characters, mixed letters and
numbers. I have another array - potentially as large - whose items I wish
to find in the first array.
A nested loop would answer this problem - and does for the time being.
for x = 0 to ubound(secondarray)
for y = 0 to ubound(firstarray)
if secondarray(x) = firstarray(y) then
itemExists = true
exit function
end if
next
next
I thought it would be better to loop the array and pass each element to a
recursive function that divides and conquers, BUT it misses about half of
the matches - every item I'm searching for already exists in the list, just
as a test. Below is the code I'm using...can anyone figure out what I'm
missing?
Thanks!
- Wm
Function ValueExists(inArray, idToCheck, lowerBound, upperBound)
If lowerBound = upperBound Then
If idToCheck = inArray(lowerBound) Then
ValueExists = True
End If
Exit Function
End If
Dim sPos
sPos = ((upperBound - lowerBound) \ 2) + lowerBound
idToCheck = Trim(idToCheck)
inArray(sPos) = Trim(inArray(sPos))
If idToCheck = inArray(sPos) Then
ValueExists = True
Exit Function
End If
If idToCheck < inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, lowerBound, sPos - 1)
Exit Function
End If
If idToCheck > inArray(sPos) Then
ValueExists = ValueExists(inArray, idToCheck, sPos + 1, upperBound)
Exit Function
End If
End Function