passing an array into a function? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
awcrt9316
Posts: 61
Joined: 03 Mar 2020, 20:06

passing an array into a function?

04 Apr 2020, 13:31

I need some way to pass an array to a function. Does anyone know how to do that?

Code: Select all

c := a7
array := {}
array["red"] := "a7"
array["orange"] := "a6"
array["blue"] := "a5"
green:= 1
purple:=0

movementdetector(green, purple, c)

movementdetector(a, b, c)
{
	if (a = 1)&&(b = 0) 
	{
	for index, element in array
	if (element = c)
	{
		MsgBox, % "Item " index " has a y value of " othervar
        }

	}

User avatar
Chunjee
Posts: 707
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: passing an array into a function?

04 Apr 2020, 14:04

Not sure what the rest of the variables are doing, but your array seems to be called "array" so functionname(array) will suffice
User avatar
boiler
Posts: 6592
Joined: 21 Dec 2014, 02:44

Re: passing an array into a function?  Topic is solved

04 Apr 2020, 14:12

See below. Note that I changed the name of the array in the function thisarray to make it clear that it's not really using the same name as the array you passed to it (although you could name it the same). It is passing a reference to the same array object. If you want the function to have its own copy of the array so that you could change it without changing the original, you would have to Clone() it.

Note that a couple errors were fixed: Added quotes in c := "a7" and replaced othervar with c because othervar wasn't referenced anywhere else.

Code: Select all

c := "a7"
array := {}
array["red"] := "a7"
array["orange"] := "a6"
array["blue"] := "a5"
green:= 1
purple:=0

movementdetector(green, purple, c, array)

movementdetector(a, b, c, thisarray)
{
	if (a = 1)&&(b = 0) 
	{
	for index, element in thisarray
	if (element = c)
	{
		MsgBox, % "Item " index " has a y value of " c
        }

	}
}

Return to “Ask For Help”

Who is online

Users browsing this forum: benton, Eik, Google [Bot], kczx3, mikeyww and 35 guests