Page 1 of 1
Trouble incrementing a global variable
Posted: 27 Apr 2016, 12:23
by omar
I have code like this:
Code: Select all
global myCounter = 2
uploadCSVFile()
{
myString = %myCounter%*.csv
Send % myString
myCounter++
}
; Press Ctrl Alt Q to launch
^!q::
uploadCSVFile()
The intention is to run the code many times.
Each time it's run, myCounter should be incremented.
What am I doing wrong?
When run a second time, it's still 2.
Thanks.
Omar
Re: Trouble incrementing a global variable
Posted: 27 Apr 2016, 12:30
by Xtra
Re: Trouble incrementing a global variable
Posted: 27 Apr 2016, 14:33
by omar
@Xtra thanks for the reply.
still the same problem.
i read up that global variables can't be changed.
so how do i do what i want to do?
Re: Trouble incrementing a global variable
Posted: 27 Apr 2016, 16:40
by Xtra
omar wrote:
i read up that global variables can't be changed.
Where did you read that?
Code: Select all
Global cnt := 1
Loop
{
ToolTip % counting()
}
counting()
{
return cnt++
}
Re: Trouble incrementing a global variable
Posted: 28 Apr 2016, 05:48
by Flarebrass
The issue is function scope. If you ran a label, you wouldn't need
Global, but a function is like a subset of code which can allow for its own local variables. For a function to recognize an outside variable, you'll need to explicitly tell it to draw from the parent thread.
I haven't messed with functions in a while in AHK, so this code might not work, but give it a shot and let me know:
Code: Select all
global myCounter = 2
; Press Ctrl Alt Q to launch
^!q::uploadCSVFile()
uploadCSVFile()
{
global myCounter
myString = %myCounter%*.csv
Send % myString
myCounter++
}
Re: Trouble incrementing a global variable
Posted: 28 Apr 2016, 06:48
by Blackholyman
@ Flarebrass
using Globel out side of a function or class will make the variable super global aka you don't need to set its scope in inside of functions, only if you wish to change its scope inside that function.
see Xtra's example or
Code: Select all
Global cnt := 1
Loop 1000
{
ToolTip % counting() " local " counting2()
}
counting()
{
return cnt++
}
counting2()
{
local cnt := 1
return ++cnt
}
Re: Trouble incrementing a global variable
Posted: 28 Apr 2016, 06:54
by evilC
If a variable declaration is outside of a function, then declaring it global at that point should make it super-global
Code: Select all
#singleinstance force
global testvar := "Hello"
myfunc1()
myfunc2()
myfunc1()
return
myfunc1(){
msgbox % "Testvar is: " testvar
}
myfunc2(){
testvar .= " World"
}
Your code works fine for me - each time I hit the hotkey, it increments myCounter by 1.
Re: Trouble incrementing a global variable
Posted: 29 Apr 2016, 07:01
by Flarebrass
I've always had problems with superglobal variables in many languages, and I remember having to debug one of my former AHK scripts for over 2 hours before finally turning the function into a label because it wouldn't recognize them. I guess I need to read up some more.
Re: Trouble incrementing a global variable
Posted: 29 Apr 2016, 07:19
by just me
It might depend on where you placed the statement. If it is placed outside of the
auto-execute section, a super-global variable will be created, but might not be initialized:
Code: Select all
#NoEnv
global myCounter = 2
Return ; end of auto-execute section ===============================================================================================
uploadCSVFile()
{
myString = %myCounter%*.csv
; Send % myString
myCounter++
Return myString
}
; Press Ctrl Alt Q to launch
^!q::
myString := uploadCSVFile()
MsgBox, myCounter: %myCounter%`nmyString: %myString%
Return
vs.
Code: Select all
#NoEnv
Return ; end of auto-execute section ===============================================================================================
global myCounter = 2
uploadCSVFile()
{
myString = %myCounter%*.csv
; Send % myString
myCounter++
Return myString
}
; Press Ctrl Alt Q to launch
^!q::
myString := uploadCSVFile()
MsgBox, myCounter: %myCounter%`nmyString: %myString%
Return
Re: Trouble incrementing a global variable
Posted: 29 Apr 2016, 07:33
by evilC
Huh, new one on me. I would have thought that myCounter would not have been super-global as the line is unreachable.