AScanT Function (Array OfT, FuncT, Logic) | |
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.19
Syntax FUNCTION AScan<T>(
aTarget AS ARRAY OF<T>,
act AS Func<T, LOGIC>
)
AS DWORD
public static uint AScan<T>(
Array Of<T> aTarget,
Func<T, bool> act
)
Request Example
View SourceParameters
- aTarget
- Type: Array OfT
The array to scan. - act
- Type: FuncT, Logic
A lambda expression that will be evaluated for every element in the array.
Type Parameters
- T
- The type of the array elements
Return Value
Type:
DWord
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.
Tip |
---|
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:
1aArray := {"Tom", "Mary", "Sue"}
2? AScan(aArray, "Mary")
3? AScan(aArray, "mary")
4? AScan(aArray, ;
5 {|x| Upper(x) = "MARY"})
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
5iAtEnd := 4
6DO WHILE (iPos := AScan(aArray, "Mary", nStart)) > 0
7 ? iPos, aArray[iPos]
8
9 IF (nStart += iPos) > iAtEnd
10 EXIT
11 ENDIF
12ENDDO
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"})
See Also