AutoHotkey Community

It is currently May 26th, 2012, 8:23 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: August 16th, 2009, 9:23 am 
Offline

Joined: March 3rd, 2005, 5:43 am
Posts: 62
Although I've been using AutoHotKey for some time now, I'm still uncertain
as to when Global and Static variables need to be used.

I'm also not real clear on the difference between them.

And, are functions the only way they can be declared. Seems like I
remember seeing somewhere that variables ending with numbers
are considered array variables and are then Global by nature.
Am I correct on that?

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 11:57 am 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
Global and static variables are both permanent, unlike local ones, but static are visible only to the function that created them. So, use them if you want this limited visibility.

All variables declared outside any function are global. Arrays can be local.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 12:23 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
is there a way to make an array global without making all the variables in the function global? i.e. without using 'global' alone as the first word in the function?

For example, suppose my array is called extraInfo, and my functions performs a loop that sometimes adds entries to it of the form extraInfo%refNumber%:= refInfo...

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 1:31 pm 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
Probably not.
Functions wrote:
Consequently, a function can create a global array manually (by means such as Array%i% := A_Index) only if it has been defined as an assume-global function.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 1:49 pm 
Quote:
is there a way to make an array global without making all the variables in the function global? i.e. without using 'global' alone as the first word in the function?


Code:
SomeFunction()
ListVars
Pause
Return

SomeFunction()
{
  Global ; all array values created are global
  Local declareTheOther := 1, variablesThat := "you don't want to be global as local"
  Loop 10
    extraInfo%A_Index% := "Field No. " . A_Index ; will be global
  declareTheOther := variablesThat ; will be local
}


HTH


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 2:10 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
n-l-i-d wrote:
Global ; all array values created are global
Local declareTheOther := 1, variablesThat := "you don't want to be global as local"


thanks. that's a clever workaround.

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 4:35 pm 
Offline

Joined: March 3rd, 2005, 5:43 am
Posts: 62
YMP wrote:
Global and static variables are both permanent, unlike local ones, but static are visible only to the function that created them. So, use them if you want this limited visibility.

All variables declared outside any function are global. Arrays can be local.


So, the reason for naming Global Variables in a function is so that their
previous values are available to the function when it's called again. Right?

I'm still a little confused by the term "Global".
Does that mean that any Hotkey routine in my script has access to a
Global variable that was created by another Hotkey Routine in my script?
..or..
Does it mean that a Global Variable is available to all Hotkey Routines in
other AutoHotKey scripts that may have been launched either earlier or later?

Thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 4:52 pm 
Larry wrote:
So, the reason for naming Global Variables in a function is so that their
previous values are available to the function when it's called again. Right?
Wrong, the reason to declaring variables global in a function is to allow the function to access variables with the same name to be accessed outside the function - essentionally this makes sure that a variable inside and outside the function are the "same" variable. If not declared global, variables inside functions are private to the function (even if a variable of the same name is used outside the function).

Larry wrote:
I'm still a little confused by the term "Global".
Does that mean that any Hotkey routine in my script has access to a
Global variable that was created by another Hotkey Routine in my script?
..or..
Does it mean that a Global Variable is available to all Hotkey Routines in
other AutoHotKey scripts that may have been launched either earlier or later?
Subroutines (including Hotkey and Timer) can only access global variabes, again 'local' function variables cannot be seen/accessed outside the function where they are created.

Static variables, are simply local variables that remember their value each time the function is entered. Otherwise 'local' variables are 'empty' each time the function is entered.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 5:22 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
@daonlyfreez

n-l-i-d wrote:
Code:
SomeFunction()
ListVars
Pause
Return

SomeFunction()
{
  Global ; all array values created are global
  Local declareTheOther := 1, variablesThat := "you don't want to be global as local"
  Loop 10
    extraInfo%A_Index% := "Field No. " . A_Index ; will be global
  declareTheOther := variablesThat ; will be local
}


HTH


Declaring Global is redundant.. It is automatic when Local is used.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 5:26 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
see if you can spot the difference:

Code:
;copy path from explorer address bar and try pasting it with the different hotkey functions
num:=1

#L::
LocalIncrement()
MsgBox % num
Return

LocalIncrement()
{
   num++
}

#G::
GlobalIncrement()
msgBox % num
Return

GlobalIncrement()
{
   global
   num++
}

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Last edited by pajenn on August 16th, 2009, 5:51 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 5:41 pm 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
Larry wrote:
Does it mean that a Global Variable is available to all Hotkey Routines in other AutoHotKey scripts that may have been launched either earlier or later?

No, they exist within one script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 5:51 pm 
YMP wrote:
Larry wrote:
Does it mean that a Global Variable is available to all Hotkey Routines in other AutoHotKey scripts that may have been launched either earlier or later?

No, they exist within one script.
YMP, Very nice catch, I completely/totally missed that! :oops:


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 7:58 pm 
Quote:
Declaring Global is redundant.. It is automatic when Local is used.


You're right, assume-global, thanks! :)


Report this post
Top
  
Reply with quote  
PostPosted: August 16th, 2009, 11:59 pm 
Offline

Joined: March 3rd, 2005, 5:43 am
Posts: 62
Thanks Everyone. Global vars are no longer a mystery to me!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Pulover, SKAN, StepO and 60 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group