Add one array to another array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Add one array to another array

26 Oct 2020, 18:59

Is the instruction for k, v in .. the only way to add one array to another array?
(like this?)

Code: Select all

#NoEnv
#SingleInstance force

aTst1 := []

aTst := tstFunc("10")
for k, v in aTst
	aTst1.push(aTst[A_Index])
	
aTst := tstFunc("100")
for k, v in aTst
	aTst1.push(aTst[A_Index])

; Show the result
MsgBox ,
( LTrim
 %	aTst1.count() "
	" aTst1.1	"
	" aTst1.2	"
	" aTst1.3	"
	" aTst1.4
)

ExitApp


tstFunc(Value)
{	Result :=[]
	Result.1 := "Result1," Value * 5
	Result.2 := "Result2," Value * 10
	Return Result
}
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Add one array to another array

26 Oct 2020, 20:20

bigA has oodles of array functions, but I never tried it.
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Re: Add one array to another array

27 Oct 2020, 03:53

Wao!
bigA .ahk seems really powerful, with many options.
But this time, I might solve it in this way

Code: Select all

#NoEnv
#SingleInstance force

aTst1 := []

aTst1 := tstFunc(aTst1, "10")
aTst1 := tstFunc(aTst1, "100")

; Show the result
MsgBox ,
( LTrim
 %	aTst1.count() "
	" aTst1.1	"
	" aTst1.2	"
	" aTst1.3	"
	" aTst1.4
)
ExitApp

tstFunc(Result, Value)
{	Result.push("Result1," Value * 5)
	Result.push("Result2," Value * 10)
	Return Result
}
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Add one array to another array

27 Oct 2020, 05:40

An alternative is below.

Code: Select all

Loop, 4
 aTst1[A_Index] := (A_Index = 1 ? 10 : aTst1[A_Index - 1]) * (A_Index / 2 = A_Index // 2 ? 2 : 5)

Code: Select all

aTst1 := tstF(10, 4, 5, 2)

tstF(start, iterations, oddFactor, evenFactor) { ; Create array, multiplying the previous value by a toggling factor
 array := []
 Loop, %iterations%
  array.Push((A_Index = 1 ? start : array[A_Index - 1]) * (A_Index / 2 = A_Index // 2 ? evenFactor : oddFactor))
 Return array
}
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: Add one array to another array

27 Oct 2020, 08:09

bigA has .concat: https://biga-ahk.github.io/biga.ahk/#/?id=concat
aCombined := A.concat(aTst, aTst2)

array.ahk also has .concat for arrays: https://chunjee.github.io/array.ahk/#/docs?id=concat
aCombined := aTst.concat(aTst2)


Anything more than that; seems like needless extra work. In the case of looping both arrays, cringe repeat-yourself programming.


ahk 2020: no concat! :shock:
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Add one array to another array  Topic is solved

27 Oct 2020, 08:22

Code: Select all

aTst1 := []
aTst1.Push( tstFunc("10")* )
aTst1.Push( tstFunc("100")* )

; Show the result
MsgBox ,
( LTrim
 %   aTst1.count() "
   " aTst1.1   "
   " aTst1.2   "
   " aTst1.3   "
   " aTst1.4
)

tstFunc(Value)
{   Result :=[]
   Result.1 := "Result1," Value * 5
   Result.2 := "Result2," Value * 10
   Return Result
}
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Add one array to another array

27 Oct 2020, 08:35

Very nice and handy, @Chunjee, though I don't know how that replicates the original output.
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: Add one array to another array

27 Oct 2020, 10:15

mikeyww wrote:
27 Oct 2020, 08:35
Very nice and handy, @Chunjee, though I don't know how that replicates the original output.
Not sure I understand the meaning of this, if anything. using https://gist.github.com/G33kDude/b12aae376d3f9dbdd857e1d92b166dc5 I verifed the OP's object and everything I exampled results in: ["Result1,50", "Result2,100", "Result1,500", "Result2,1000"]
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Add one array to another array

27 Oct 2020, 10:32

OK. I stand corrected.
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Re: Add one array to another array

27 Oct 2020, 17:14

Thank you! (every one)

Right now I think the solution from @teadrinker seems easiest to use and understand.
(but I don't know what the "*" in the function() call aTst1.Push( tstFunc("10")* )) does.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Add one array to another array

27 Oct 2020, 18:06

Some cool scripting there!
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Re: Add one array to another array

27 Oct 2020, 19:02

But I came across another surprise (for me)

In this example an array (aTst1) is created (in the main program)
This array (aTst1) is sent to the Function() to another array (Res)
A calculation is performed in the Function() and saved in the array Res
The Function() is finished.
At last I want to check the values in aTst1

I was suprised that the Tst1.2 has ben changed (in the Function()).
Why?
(What have I missed?)

Code: Select all

#NoEnv
#SingleInstance force

aTst1 := []
aTst1.1 := "10"
aTst1.2 := "20"

aRes := tstFunc(aTst1)

; Show the result
MsgBox ,, %A_ScriptName% - Row %A_LineNumber% ,
( LTrim
 %	"Array no .: " aTst1.count() "
	" aTst1.1	"
	" aTst1.2
)
ExitApp

tstFunc(Res)
{	a1 := Res.1
	a2 := Res.2
	
	Res.2 := a1 * 10
	MsgBox ,, %A_ScriptName% - Row %A_LineNumber%, % Res.2

	Return
}
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Add one array to another array

27 Oct 2020, 19:17

Albireo wrote: (but I don't know what the "*" in the function() call aTst1.Push( tstFunc("10")* )) does.
In this case the asteriks doesn't relate to the function call, it relates to what the function returns.

Code: Select all

Arr1 := [1, 2]
Arr2 := [3, 4]
Arr1.Push(Arr2*)

for k, v in Arr1
   items .= (items ? ", " : "") . v
MsgBox, % items
Albireo wrote: This array (aTst1) is sent to the Function() to another array (Res)
An array is always sent to a function by reference, so aTst1 and Res are the same array.

Code: Select all

Arr := ["key"]
MyFunc(Arr)
MsgBox, % Arr[1]

MyFunc(Obj) {
   Obj[1] := "value"
}
Albireo
Posts: 1778
Joined: 16 Oct 2013, 13:53

Re: Add one array to another array

28 Oct 2020, 02:47

Thank you!
teadrinker wrote:
27 Oct 2020, 19:17
...
An array is always sent to a function by reference, so aTst1 and Res are the same array.
...
That explains a lot (why my function only works once - the first time)
Is the only way to handle this, to use local variables in the function?

Code: Select all

#aPos := []
aPos.x1 := 100
aPos.y1 := 200
aPos.x2 := 800
aPos.y2 := 1200

tstFunc(aPos)

; Show the result
MsgBox ,, %A_ScriptName% - Row %A_LineNumber% ,
( LTrim
 %	"Array no .: " aPos.count() "
	" aPos.x1	"
	" aPos.y1	"
	" aPos.x2	"
	" aPos.y2
)
ExitApp

tstFunc(aRes)
{	
	x1 := aRes.x1
	y1 := aRes.y2	
	x2 := aRes.x3
	y2 := aRes.y4
	
	x1 += 50
	y2 += 200

	Return
}
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Add one array to another array

28 Oct 2020, 05:24

To get another object from existing one you can use the Clone() method.

Code: Select all

Arr := {key: "value"}
AnotherArr := Arr.Clone()
MsgBox, % AnotherArr.key

AnotherArr.key := "anotherValue"
MsgBox, % Arr.key "`n" AnotherArr.key

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 384 guests