Add 1D array to existing 2D array

ε祈祈猫儿з 提交于 2019-12-11 04:29:09

问题


I want to merge 1D arrays. How to change below AutoIt script to access elements as a newly generated 2D array? Test script is :

#Include <Array.au3>

Local $_arr1=["name1","addr1","phone1"]
Local $_arr2=["name2","arr2","phone2"]
_make2darray($_arr1,$_arr2)

Func _make2darray($_arr1,$_arr2)
    Local $_2darray=[$_arr1,$_arr2]
    _ArrayDisplay($_2darray)

    _ArrayDisplay($_2darray[0])
    _ArrayDisplay($_2darray[1])

    ConsoleWrite($_2darray[0][0])
EndFunc

$_2darray output is:

Row | Col 0
[0] |{Array}
[1] |{Array}

$_2darray[0] output is:

Row |Col 0
[0] |name1
[1] |addr1
[2] |phone1

$_2darray[1] output is:

Row |Col 0
[0] |name2
[1] |arr2
[2] |phone2

But an error occurs accessing the 2D array:

ConsoleWrite($_2darray[0][0])
ConsoleWrite(^ ERROR
Exit code: 1    Time: 239.1

How can I fix this?


回答1:


… 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.




回答2:


You made a 1D array filled with other 1D arrays.

#Include <Array.au3>

Local $_arr1=["name1","addr1","phone1"]
Local $_arr2=["name2","arr2","phone2"]
Local $_2dArray = [$_arr1 , $_arr2]

_ArrayDisplay($_2dArray[0])
_ArrayDisplay($_2dArray[1])


来源:https://stackoverflow.com/questions/48742401/add-1d-array-to-existing-2d-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!