How do I merge/push new array to existing multi-dimensional array? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nutnutwin
Posts: 76
Joined: 28 Aug 2019, 07:25

How do I merge/push new array to existing multi-dimensional array?

Post by nutnutwin » 05 Dec 2022, 22:17

Hi, thank you for your time

# My Situation

I currently have a multi-dimensional array(2 dimensional) with several arrays as member

# What I did

Try to add new array to the 2d-array

# my source code

Code: Select all

 ; Case1: ✔️Works
	Local_Array_2D = % [["a1", "a2", "a3", "hei"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]
	MsgBox, % Local_Array_2D[1, 3]

; Case2: ❌Not Works
	Local_Array_1 = % ["a1", "a2", "a3", "hei"]
	Local_Array_2 = % ["b1", "b2", "b3"]
	Local_Array_3 = % ["c1", "c2", "c3"]

	Local_Array_2D_2.Push(Local_Array_1)
	MsgBox, % Local_Array_2D_2[1, 3]

In Case1(ready made 2d array) I can access its member
In Case2(push new member) I cannot access its member

# My question
is it grammarly correct to push array into 2d-array?
I can copy and overwrite member one by one but wonder if there are smarter way to do so without using brute force

# MISC
I googled for an hour and searched on forum, but found no answers

Thank you

User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: How do I merge/push new array to existing multi-dimensional array?  Topic is solved

Post by Datapoint » 05 Dec 2022, 22:36

Code: Select all

Local_Array_1 := ["a1", "a2", "a3", "hei"]
Local_Array_2D_2 := []
Local_Array_2D_2.Push(Local_Array_1)
MsgBox, % Local_Array_2D_2[1, 3]
I think the only problem with your script is that you need to create the array first before you .Push() to it.

User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: How do I merge/push new array to existing multi-dimensional array?

Post by boiler » 05 Dec 2022, 23:39

I'd also add that although it works, using the legacy assignment operator = followed by forcing an expression with % is pretty gross compared to using the expression assignment operator :=.

User avatar
Chunjee
Posts: 1418
Joined: 18 Apr 2014, 19:05
Contact:

Re: How do I merge/push new array to existing multi-dimensional array?

Post by Chunjee » 06 Dec 2022, 00:31

Local_Array_2D_2.Push(Local_Array_1) fails because Local_Array_2D_2 is not yet an object and therefore has no access to the .push method.

Code: Select all

; Case2: ✔️Fixed
Local_Array_2D_2 := []
Local_Array_2D_2.push(["a1", "a2", "a3", "hei"])
msgbox, % Local_Array_2D_2[1, 3]
; => "a3"


Checkout https://biga-ahk.github.io/biga.ahk/#/?id=merge for all your merging needs

nutnutwin
Posts: 76
Joined: 28 Aug 2019, 07:25

Re: How do I merge/push new array to existing multi-dimensional array?

Post by nutnutwin » 06 Dec 2022, 16:42

@Datapoint
Hi, Thank you for your reply
I thought at first it might be a init problem, but I tried

Code: Select all

Local_Array_2D_2 := [[]]
since I thought it is a 2d array(awkward

@boiler
Yes indeed it is not pretty
The problem here is in function return I use % in order to avoid confusion(expression or string), for example

Code: Select all

Return % Func_B
thus I thought it would better to use % to force expression everywhere
Will definitely do the refactoring

@Chunjee
Appreciate your info. will refer to the git site

Thank you all for your help!

User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: How do I merge/push new array to existing multi-dimensional array?

Post by boiler » 06 Dec 2022, 18:22

nutnutwin wrote: The problem here is in function return I use % in order to avoid confusion(expression or string), for example

Code: Select all

Return % Func_B
thus I thought it would better to use % to force expression everywhere
The % is not needed there either. What follows Return is always an expression. I know it's hard to keep track of all this stuff, so I just point it out when I see it, as I would have found it helpful when I was learning this stuff. The good thing looking forward is v2 is always expressions everywhere.

Post Reply

Return to “Ask for Help (v1)”