Jump to content
AlexIsProgramming

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])

 

Share this post


Link to post
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 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
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?

Share this post


Link to post
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

 

Share this post


Link to post
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

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

@Nine
You're right. The parameter, ByRef $_array, is the local copy of the array, $array_1[2] in the function. There is no need to create another array in the function.

Share this post


Link to post
Share on other sites
2 hours ago, Nine said:

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

 

That's far better than my solution 😂 Thank you all for your help!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...