Click or drag to resize

AScanT Function (Array OfT, T, Int32)

Scan an array until a value is found or a code block returns TRUE.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.10
Syntax
 FUNCTION AScan<T>(
	aTarget AS ARRAY OF<T>,
	uSearch AS T,
	nStart AS LONG
)
WHERE T : NEW()
 AS DWORD
View Source

Parameters

aTarget
Type: Array OfT
The array to scan.
uSearch
Type: T
The value to scan for.  Unless this argument is a code block, it must match the data type of the elements in <aTarget>.
nStart
Type: Int32
The starting element.  A negative value starts from the end.  If <nCount> is  positive, the default value is 1; if <nCount> is negative, the default value is the length of the array.

Type Parameters

T
The type of the array elements

Return Value

Type: UInt32
If <uSearch> is a code block, AScan() returns the position of the first element for which the code block returns TRUE.  Otherwise, AScan() returns the position of the first matching element.  AScan() returns 0 if no match is found.
Remarks
If <uSearch> is not a code block, its value is compared to the first target element.  If there is no match, AScan() proceeds to the next element in the array.  This process continues until either a match is found or the range of elements to scan is exhausted. If <uSearch> is a code block, AScan() scans <aTarget>, executing the code block for each element accessed.  As each element is encountered, AScan() passes the element's value as an argument to the code block, then performs an Eval() on the code block.  The scanning operation stops when the code block returns TRUE or when the range of elements to scan is exhausted. Note:  Values are compared using the = operator.  For exact matching (that is, using the == operator), use AScanExact().
Examples
This example demonstrates scanning a 3-element array, using both a string and a code block as search criteria.  The code block criteria shows how to perform a case-insensitive search:
X#
aArray := {"Tom", "Mary", "Sue"}
? AScan(aArray, "Mary")                                // 2
? AScan(aArray, "mary")                                // 0
? AScan(aArray, ;
                {|x| Upper(x) = "MARY"})        // 2
This example demonstrates scanning for multiple instances of a search argument after a match is found:
X#
LOCAL aArray := {"Tom", "Mary", "Sue", "Mary"}
LOCAL nStart := 1
LOCAL iAtEnd, iPos AS INT
// Get last array element position
iAtEnd := 4
DO WHILE (iPos := AScan(aArray, "Mary", nStart)) > 0
        ? iPos, aArray[iPos]
        // Get new starting position and test boundary condition
        IF (nStart += iPos) > iAtEnd
                EXIT
        ENDIF
ENDDO
This example scans a 2-dimensional array using a code block.  Note that the argument aVal in the code block is an array:
X#
LOCAL aArr := {}
AAdd(aArr,{"one", "two"})
AAdd(aArr,{"three", "four"})
AAdd(aArr,{"five", "six"})
? AScan(aArr, {|aVal| aVal[2] = "four"})                // 2
See Also