Change array value from within "for loop"? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Change array value from within "for loop"?

27 Sep 2021, 06:18

Is this a bug or feature, that you cannot change the value from within for loop?

Code: Select all

myArr := ["one", "two", "three"]

for , val in myArr {
	val := 1
}

for , val in myArr {
	msgbox val
}
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Change array value from within "for loop"?  Topic is solved

27 Sep 2021, 06:27

It's not a bug. It's documented behavior. The variable val is just a variable containing the value of the array element, not a pointer to the array element.
v2 docs For Loop wrote:The variables in which to store the values returned by the enumerator at the beginning of each iteration.

You can change the value of an array element in the loop, but not by changing the value of that temporary loop variable.

Code: Select all

myArr := ["one", "two", "three"]

for key, val in myArr {
	myArr[key] := 1
}

for , val in myArr {
	msgbox val
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Archimede, Giresharu, Nishanth, Noitalommi_2, songdg and 29 guests