Click or drag to resize

AScanT Function (Array OfT, FuncT, Boolean)

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.6
	aTarget AS ARRAY OF<T>,
	act AS Func<T, LOGIC>
Request Example View Source


Type: Array OfT
The array to scan.
Type: FuncT, Boolean
A lambda expression that will be evaluated for every element in the array.

Type Parameters

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
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().
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:
1aArray := {"Tom", "Mary", "Sue"}
2? AScan(aArray, "Mary")                // 2
3? AScan(aArray, "mary")                // 0
4? AScan(aArray, ;
5        {|x| Upper(x) = "MARY"})    // 2
This example demonstrates scanning for multiple instances of a search argument after a match is found:
 1LOCAL aArray := {"Tom", "Mary", "Sue", "Mary"}
 2LOCAL nStart := 1
 3LOCAL iAtEnd, iPos AS INT
 4// Get last array element position
 5iAtEnd := 4
 6DO WHILE (iPos := AScan(aArray, "Mary", nStart)) > 0
 7    ? iPos, aArray[iPos]
 8    // Get new starting position and test boundary condition
 9    IF (nStart += iPos) > iAtEnd
10        EXIT
11    ENDIF
This example scans a 2-dimensional array using a code block.  Note that the argument aVal in the code block is an array:
1LOCAL aArr := {}
2AAdd(aArr,{"one", "two"})
3AAdd(aArr,{"three", "four"})
4AAdd(aArr,{"five", "six"})
5? AScan(aArr, {|aVal| aVal[2] = "four"})        // 2
See Also