Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

FAQ: Variables, dynamic variables, literal strings, and stuff like that


  • Please log in to reply
5 replies to this topic
Leo Xiong
  • Members
  • 140 posts
  • Last active: Mar 15 2014 06:42 AM
  • Joined: 13 Apr 2010

Being on the IRC channel frequently, I've seen countless variable related questions. So I'll try and clear things up here. If there is something that hasn't been covered or unclear, please identify it and tell me in this thread.

But firstly, if you haven't read the documentation on variables, please do so.

 

Contents

Variable assignment

Passing/referencing variables

Comparing variables

 

Variable assignment

Literal assignment - assigning a string to a variable

variable = literal string
variable := "literal string"

Variable Assignment - assigning a variable to a variable

variable = %another_variable%
variable := another_variable 

Expression assignment - assigning a expression (expressions include variables, concatenation, mathematical and bitwise operations, ternary statements, function returns, literal strings) to a variable

variable := 1 + 1 ;evaluates to 2
variable := another_variable
variable := condition ? "true" : "false"
variable := chr(asc("a") ^ 32)

Concatenation - combining variables and strings together

variable := another_variable . " literal text" ;The period is the concatenation symbol, it can be omitted like below
variable := another_variable " literal text" ;Spaces are ignored in expressions
variable = %another_variable% literal text

Dynamic variable assignment - assigning a string or expression to a dynamic variable

number = 1
ball%number% = red ;equilvelent to ball1 = red

Passing/referencing variables

Function parameters by default forces expressions.

Passing to functions - function parameters by default are expressions

function("literal string") ;literal string
function(variable) ;variable
function(1 + 1) ;expression
function("1+1=" 1 + 1) ;concatenation of string and expression
function(ball%number%) ;passing a dynamic variable, equilvelent to function(ball1)

Passing to commands

MsgBox, literal string ;literal string
MsgBox, %variable% ;variable
MsgBox, % 1 + 1 ;force expression
MsgBox, % "1+1=" 1 + 1 ;force expression and concatenate string and math operation
MsgBox, % ball%number% ;reference dynamic variable

Reference dynamic variables (pseudo-arrays)

number = 1
ball1 = red
ball2 = orange
ball3 = yellow
MsgBox, % ball%number% ;equilvelent to MsgBox, % ball1

Example application

keys = abcdefghijklmnopqrstuvwxyz
StringSplit, keys, keys

Loop, %keys0%
	Hotkey, % keys%A_Index%, keydown

return
keydown:
ToolTip, %A_ThisHotkey% was pressed

Calling dynamic functions

color = red

MsgBox, %color%()

red(){
	return 0xff0000
}

green(){
	return 0x00ff00
}

blue(){
	return 0x0000ff
}
Comparing variables

Variable comparison - comparing two variables

answer = fourtytwo
guess = fourtytwo

if (guess = answer)
    MsgBox, You are correct
String comparison - comparing a variable with a string

operatingSystem := A_OSVersion
 
if (operatingsystem = "win_8") ;NB: the single = sign means this is *NOT* case sensitive
    MsgBox, This program does not support Windows 8
operatingSystem := A_OSVersion
 
if (operatingSystem == "win_8") ;This *is* case sensitive and will make this expression false
    MsgBox, This program does not support Windows 8

gplus-16.pngLeo Xiong.com
#ahk freenode - catch me there as NameLess-exe!

hoppfrosch
  • Members
  • 399 posts
  • Last active: Feb 26 2016 05:31 AM
  • Joined: 25 Jan 2006

looks fine - unless I (personally) would recommend to use ":=" assigments

 

Concatenation - combining variables and strings together
variable := another_variable " literal text"
variable = %another_variable% "literal text"

 

Those two differ! After assignment, the variable in the first example does NOT include the quotes - but within the second example it does....

Correct:

variable := another_variable " literal text"
variable = %another_variable% literal text


Leo Xiong
  • Members
  • 140 posts
  • Last active: Mar 15 2014 06:42 AM
  • Joined: 13 Apr 2010

looks fine - unless I (personally) would recommend to use ":=" assigments

 

Those two differ! After assignment, the variable in the first example does NOT include the quotes - but within the second example it does....

Same, but I see lots of people using it, so good to know what it does. And thanks for pointing that out :).


gplus-16.pngLeo Xiong.com
#ahk freenode - catch me there as NameLess-exe!

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
good tutorial, although i too would have left out the = altogether and just used :=. its just too confusing for new ppl. AHKv2 will do away with =

anyway, just some quirks:
 

Expression assignment - assigning a expression (expressions include variables, concatenation, mathematical and bitwise operations, ternary statements, function returns, literal strings) to a variable

variable := 1 + 1 ;evaluates to 2
variable := another_variable
variable = %another_variable%
variable := condition ? "true" : "false"
variable := chr(asc("a") ^ 32)


#3 above is not an expression. its a dereference and an assignment. AHKv2 will allow expressions inside the %% like so MsgBox, 1+1= %1+1%
 

Concatenation - combining variables and strings together

variable := another_variable " literal text"
variable = %another_variable% literal text


you should probably put a note that your example are for auto-concatenation, which is a specific shorthand feature where you can just use a space instead of the actual concatenation operator, which is a period .
 
variable := another_variable . " literal text"


Leo Xiong
  • Members
  • 140 posts
  • Last active: Mar 15 2014 06:42 AM
  • Joined: 13 Apr 2010

Yep, I haven't actually actually dabbed into AHKv2, so this document is more for AHKv1.


gplus-16.pngLeo Xiong.com
#ahk freenode - catch me there as NameLess-exe!

JnLLnd
  • Members
  • 193 posts
  • Last active: Jul 23 2015 02:15 AM
  • Joined: 30 Dec 2007

Good idea this reference card on variables.  Just a clarification:

 

Dynamic variable assignment - assigning a string or expression to a dynamic variable

number = 1

ball%number% = red ;equilvelent to ball1 = red

 

What you call here "dynamic variable" is refered in the documentation as "pseudo-arrays".

 

Thanks you Leo.