Jump to content

Assign value within multidimensional arrays doesn't work


Recommended Posts

Hello guys,

i've got a problem assigning a single value within a multidimensional array. I want to change the subarray value in $array_1[2] from 4 to 9. Can anyone tell me where is my fallacy is?

#include <Array.au3>

Local $array_1[3] = [1,2]
Local $array_2[3] = [4,5,6]

$array_1[2] =$array_2

_ArrayDisplay($array_1)
_ArrayDisplay($array_1[2])
($array_1[2])[0] = 9
_ArrayDisplay($array_1[2])

 

Link to comment
Share on other sites

It doesn't work this way, IIRC.
You have to extract the subarray, modify it and then upate the array with the modified subarray.
See this wiki tutorial for details.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

To clarify, I don't want to change the value in $array_2. I want to change the value stored in $array_1. $array_2 is just for building up the 'working array'.

I can't use the normal way like this:

#include <Array.au3>

Local $array_1[3][3] = [[1],[2],[4,5,6]]
;~ Local $array_2[3] = [4,5,6]

;~ $array_1[2] =$array_2

_ArrayDisplay($array_1)
$array_1[2][0] = 9
_ArrayDisplay($array_1)

In my programm $array_1 holds all GUICTRL-ID's and at some point I want to change the assignd value. I know Arrays in Arrays is hard work, but so far the benefits are worth it.

The question is, why can i only read the value but not change it. Is there really no way other than extract and write again?

Link to comment
Share on other sites

25 minutes ago, water said:

It doesn't work this way, IIRC.
You have to extract the subarray, modify it and then upate the array with the modified subarray.
See this wiki tutorial for details.

If the only way to change the value 'extract, modify, update' is, this would be my workaround. But this can't the solution right?

#include <Array.au3>

Local $array_1[3] = [1,2]
Local $array_2[3] = [4,5,6]

$array_1[2] =$array_2

_ArrayDisplay($array_1)
_ArrayDisplay($array_1[2])

$array_1[2] = ChangeArrayInArray($array_1[2],0,9)

_ArrayDisplay($array_1[2])


Func ChangeArrayInArray($_array, $_row, $_value)
   Local $arr[0]
   For $i = 0 To Ubound($_array)-1 Step +1
      If $i = $_row Then
         _ArrayAdd($arr,$_value)
      Else
         _ArrayAdd($arr,$_array[$i])
      EndIf
   Next
   Return $arr
EndFunc

 

Link to comment
Share on other sites

Here's another way.

#include <Array.au3>

Local $array_1[3] = [1, 2]
Local $array_2[3] = [4, 5, 6]

$array_1[2] = $array_2

_ArrayDisplay($array_1)
_ArrayDisplay($array_1[2])

ChangeArrayInArray($array_1[2], 0, 9)

ConsoleWrite(($array_1[2])[0] & @CRLF)  ; Returns 9
_ArrayDisplay($array_1[2])


Func ChangeArrayInArray(ByRef $_array, $_row, $_value)
    Local $arr = $_array
    $arr[$_row] = $_value
    $_array = $arr
EndFunc   ;==>ChangeArrayInArray

 

Link to comment
Share on other sites

No need to local copy the array (for large array it will slow down the process), since it is ByRef, just use :

Func ChangeArrayInArray(ByRef $_array, $_row, $_value)
    $_array[$_row] = $_value
EndFunc   ;==>ChangeArrayInArray

 

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