AlexIsProgramming Posted March 9, 2020 Share Posted March 9, 2020 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 More sharing options...
water Posted March 9, 2020 Share Posted March 9, 2020 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
AlexIsProgramming Posted March 9, 2020 Author Share Posted March 9, 2020 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 More sharing options...
AlexIsProgramming Posted March 9, 2020 Author Share Posted March 9, 2020 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 More sharing options...
Malkey Posted March 9, 2020 Share Posted March 9, 2020 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 More sharing options...
Nine Posted March 9, 2020 Share Posted March 9, 2020 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 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Malkey Posted March 9, 2020 Share Posted March 9, 2020 @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. Link to comment Share on other sites More sharing options...
AlexIsProgramming Posted March 9, 2020 Author Share Posted March 9, 2020 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! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now