Click or drag to resize

ALen Function (Array)

X#
Return the number of elements in an array.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.19
Syntax
 FUNCTION ALen(
	aTarget AS ARRAY
) AS DWORD
Request Example View Source

Parameters

aTarget
Type: Array
The array to count.

Return Value

Type: DWord
The number of elements in the array.
If the array is empty, ALen() returns 0.
Remarks
If the array is multidimensional, subarrays count as one element.
This means that the ALen() of a multidimensional array simply returns the length of the first dimension.
To determine the number of elements in other dimensions, use ALen() on the subarrays, as shown in the example below. (Note that multidimensional arrays in X# need not have uniform dimensions.)
Examples
This example creates a literal 2-dimensional array, then returns the array and the number of elements in the subarray that is contained in the first element of the array:
X#
1LOCAL aArray := {{1, 2}, {1, 2}, {1, 2}}
2? ALen(aArray)                                // 3
3? ALen(aArray[1])                            // 2
This example navigates a multidimensional array using ALen():
X#
 1LOCAL aArray := {{1, 2}, {1, 2}, {1, 2}}
 2LOCAL nRow, nColumn AS SHORTINT
 3LOCAL nRowCount, nColumnCount AS SHORTINT
 4nRowCount = ALen(aArray)
 5FOR nRow := 1 UPTO nRowCount
 6    nColumnCount = ALen(aArray[nRow])
 7    FOR nColumn := 1 UPTO nColumnCount
 8        ? nRow, nColumn, aArray[nRow][nColumn]
 9    NEXT
10NEXT
In this example, a function returns an array of numeric values that describe the dimensions of a multidimensional array.
The function assumes that the array has uniform dimensions:
X#
1FUNCTION Dimensions(aArray)
2    LOCAL aDims := {}
3    DO WHILE IsArray(aArray)
4        AADD(aDims, ALen(aArray))
5        aArray := aArray[1]
6    ENDDO
7    RETURN (aDims)
See Also