… an error occurs accessing the 2D array …
It assigns arrays to another 1D array's elements, which can only be accessed isolated; like:
$aArray = $_2dArray[0]
_ArrayDisplay($aArray)
or just _ArrayDisplay($_2dArray[0])
. But then addresses this as if it were a 2 dimensional array, hence the Array variable has incorrect number of subscripts or subscript dimension range exceeded.
-error.
How can I fix below AutoIt script to allow accessing elements of the newly generated 2D array?
As per Documentation - Keywords - ReDim
:
Resize an existing array.
Example:
#include <AutoItConstants.au3>; UBound() constants.
#include <Array.au3>; _ArrayDisplay()
Global Const $g_aArray1D_1 = ['name1', 'address1', 'phone1']
Global Const $g_aArray1D_2 = ['name2', 'address2', 'phone2']
Global $g_aArray2D = [['NAME', 'ADDRESS', 'PHONE'] ]
_ArrayAdd1DtoArray2D($g_aArray2D, $g_aArray1D_1)
_ArrayAdd1DtoArray2D($g_aArray2D, $g_aArray1D_2)
_ArrayDisplay($g_aArray2D)
Func _ArrayAdd1DtoArray2D(ByRef $aArray2D, Const $aArray1D)
Local Const $iRows = UBound($aArray2D, $UBOUND_ROWS)
Local Const $iCols = UBound($aArray2D, $UBOUND_COLUMNS)
; Resize array:
ReDim $aArray2D[$iRows + 1][$iCols]
For $i1 = 0 To $iCols - 1
; Add values of 1D array to new row of 2D array:
$aArray2D[$iRows][$i1] = $aArray1D[$i1]
Next
EndFunc
Or using _ArrayAdd()
(converts to strings) :
#include <Array.au3>; _ArrayToString() _ArrayAdd() _ArrayDisplay()
Global Const $g_aArray1D_1 = ['name1', 'address1', 'phone1']
Global Const $g_aArray1D_2 = ['name2', 'address2', 'phone2']
Global $g_aArray2D = [['NAME', 'ADDRESS', 'PHONE'] ]
_ArrayAdd($g_aArray2D, _ArrayToString($g_aArray1D_1))
_ArrayAdd($g_aArray2D, _ArrayToString($g_aArray1D_2))
_ArrayDisplay($g_aArray2D)
Values are accessible using $g_aArray2D[ x ][ x ]
now.