Prblem with Loop and A_Index global variable Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
megabytez2
Posts: 16
Joined: 19 Feb 2021, 04:56

Prblem with Loop and A_Index global variable

18 Oct 2021, 02:29

Hi all,

Was wondering if anyone could help with the following issue?
It's a simple issue but I've spent hours trying to find an answer without luck :(

I have the following loop code that sets a variable based on the A_Index.

Code: Select all

global looprepeat = 4
Loop, %looprepeat% {
Item%A_Index% = LoopNo%A_Index%
}
How do I make the variable Item%A_Index% global? i.e. Item2, Item3, Item4.

I tried putting global before Item%A_Index% but it gives me an error message
"The following variable name contains an illegal character : "GroupMemberName%A_Index%"
Last edited by megabytez2 on 18 Oct 2021, 02:40, edited 1 time in total.
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Prblem with Loop and A_Index global variable  Topic is solved

18 Oct 2021, 02:37

Put this loop inside a function and add a Global word to in top of it.
Something like this

Code: Select all

Func() {
    global
    looprepeat = 4
	Loop, %looprepeat% {
		Item%A_Index% = Answer%A_Index%
	}
}
megabytez2
Posts: 16
Joined: 19 Feb 2021, 04:56

Re: Prblem with Loop and A_Index global variable

18 Oct 2021, 02:44

Smile_ wrote: Put this loop inside a function and add a Global word to in top of it.
Something like this

Code: Select all

Func() {
    global
    looprepeat = 4
	Loop, %looprepeat% {
		Item%A_Index% = Answer%A_Index%
	}
}
Thanks! I'll give it a go and let you know
megabytez2
Posts: 16
Joined: 19 Feb 2021, 04:56

Re: Prblem with Loop and A_Index global variable

18 Oct 2021, 02:45

Thank you! I'll give it a go and post an update.
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Prblem with Loop and A_Index global variable

18 Oct 2021, 03:24

... or use one super-global simple array:

Code: Select all

Global looprepeat := 4
Global itemarray := []
Loop, %looprepeat% {
	itemarray[A_Index] := "LoopNo" . A_Index
}
To retrieve the values use itemarray[A_Index] instead of item%A_Index%.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 337 guests