Trouble incrementing a global variable

Get help with using AutoHotkey and its commands and hotkeys
omar
Posts: 451
Joined: 22 Oct 2015, 17:56

Trouble incrementing a global variable

27 Apr 2016, 12:23

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
User avatar
Xtra
Posts: 2038
Joined: 02 Oct 2015, 12:15

Re: Trouble incrementing a global variable

27 Apr 2016, 12:30

use:

Code: Select all

global myCounter := 2
omar
Posts: 451
Joined: 22 Oct 2015, 17:56

Re: Trouble incrementing a global variable

27 Apr 2016, 14:33

@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?
User avatar
Xtra
Posts: 2038
Joined: 02 Oct 2015, 12:15

Re: Trouble incrementing a global variable

27 Apr 2016, 16:40

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++
}

User avatar
Flarebrass
Posts: 104
Joined: 20 Nov 2015, 13:13
Location: USA
Contact:

Re: Trouble incrementing a global variable

28 Apr 2016, 05:48

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++
}
(Note that I can't test my code before posting, so beware of bugs! -Flarebrass Amatzikahni)
User avatar
Blackholyman
Posts: 1292
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: Trouble incrementing a global variable

28 Apr 2016, 06:48

@ 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
}
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
evilC
Posts: 4787
Joined: 27 Feb 2014, 12:30

Re: Trouble incrementing a global variable

28 Apr 2016, 06:54

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.
User avatar
Flarebrass
Posts: 104
Joined: 20 Nov 2015, 13:13
Location: USA
Contact:

Re: Trouble incrementing a global variable

29 Apr 2016, 07:01

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.
(Note that I can't test my code before posting, so beware of bugs! -Flarebrass Amatzikahni)
just me
Posts: 7604
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Trouble incrementing a global variable

29 Apr 2016, 07:19

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

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Elish, TAC109, Wigi, wineguy and 39 guests