Jump to content

modify element of Array inside a 2d Array


frank10
 Share

Recommended Posts

I create a 2D Array with another Array inside it.

I can read single rows of the inner array [encapsulating the outer Array with (...) ], but I can't change them, how to do this?

local $aHeader = [ 0, 4 ]
global $aData = [[1, $aHeader]]
                    
MsgBox(0,'', ($aData[0][1])[1] ) ; read ok --> 4

($aData[0][1])[0] = 2 ; --> ERR, how to modify the arr inside the $aData 2d Arr?

 

EDIT:

Also, I can't insert the array inside the 2d Arr without using an intermediate var...

$aData[0][1] = $aHeader ; ok            

$aData[0][1] = [1,2]  ; ERR

 

Edited by frank10
Link to comment
Share on other sites

You cannot set it like that , you need to use an array to perform the modification :

#include <Array.au3>

local $aHeader = [ 0, 4 ]
Global $aData = [[1, $aHeader]]

MsgBox(0,'', ($aData[0][1])[1] ) ; read ok --> 4

Local $aTmp = $aData[0][1]
$aTmp[1] = 2
$aData[0][1] = $aTmp

_ArrayDisplay($aData[0][1])

SetArr($aData[0][1], 0, 3)
_ArrayDisplay($aData[0][1])

Func SetArr (ByRef $aArr, $i, $v)
  $aArr[$i] = $v
EndFunc

 

Link to comment
Share on other sites

Another way to do it :

local $aHeader = [ 0, 4 ]
global $aData = [[1, $aHeader]]

MsgBox(0,'', ($aData[0][1])[1] ) ; read ok --> 4

; ($aData[0][1])[0] = 2 ; --> ERR, how to modify the arr inside the $aData 2d Arr?
_ASA($aData[0][1], 1, 2)

MsgBox(0,'', ($aData[0][1])[1] ) ; read ok --> 2

;  --------- Assign to Sub-Array (ASA) ---------
Func _ASA(ByRef $array, $iIndex, $Value)
    $array[$iIndex] = $Value ; Assign the new value.
EndFunc   ;==>_ASA

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...