Searching large arrays repetitively can result in
unnecessary overheads, but this simple approach can reduce the workload of
the search mechanism. The only way to find data in an array is to loop
through each element until a match is found. An advantage of employing a
loop is that the loop counter holds the last loop index whether the loop
is exited or completed. Therefore if the index is great than the loops
maximum value this indicates a match was not found. Also if a match was
found the loop counter now identifies the array element. The example below
demonstrates populating a FlexGrid's second column from an array: 'loop thru each row in the FlexGrid
For intLoop = 0 to flx.Rows -1
'loop thru array
For intCount = 0 to UBound(arrSupplies)
'if a match is found stop searching array
If arrSupplies(intCount).ID = flx.TextMatrix(intLoop, 0) Then
Exit For
End If
'as intCount vale is retained, interrogate to
'determine if a match found
If intCount > UBound(arrSupplies) Then
flx.TextMatrix(intLoop, 1) = "N/A"
Else
flx.TextMatrix(intLoop, 1) = arrSupplies(intCount).Desc
End If
Next intCount
Next intLoop
Another tip, the above example assumes the grid has far less rows than the
array has elements, but if the array is small and the FlexGrid large, then
the outer loop should step through the array and the inner loop the
FlexGrid. The intention being the largest loop has the potential to be
exited from with the Exit For statement, thereby reducing the
number of steps in the overall search. |