When you create variables, you’re telling the computer to find a place in memory and store a value there.
Later, when you update / retrieve from the variable, the computer knows to look at that place in memory.
Let me provide an example, assume you want to store two values (20 and 10) in your script and, at a later stage, you want to use these two values.
Let’s go over how you do it.
Here are the three simple steps −
- Create two variables with appropriate names.
- Store your values in those two variables.
- Retrieve and use the stored values from the variables.
Creating variables is more commonly called “declaring variables” in programming as you need to declare your variables before use.
Different programming languages have different ways of creating/declaring variables inside a program/script.
In AutoHotkey you don’t need to declare your variables before use, you can simply create them when needed
For example, AutoHotkey has the following simple way of creating variables −
a := “” b := “”
The above program creates two variables, behind the scenes the AHK interpreter is reserving two memory locations that we now know by the names a and b.
We created these variables using (:= “”) to specify an empty string using an expression, remember that in AutoHotkey you do not need to create variables before use and it let’s you store all types of values in your variables.
Similarly, you can easily create variables to store number, floats, Objects or any other data type no extra work needed.
For example −
; variable that stores a string a := “Hello” ; variable that stores a float value b := 10.5
Listed below are the key points about variables that you need to keep in mind −
- The AutoHotkey language does not require variable declaration, i.e., creating before its usage in your program. You can use a variable name in your program without creating it, though other programming language like C does not allow you to use a variable name without declaring it.
- You can use the same variable name once inside your programs global space. For example, if a variable a has been used to store a value, then you cannot use a again to store any other type of value unless you wish it to overriding the content of a.
- There are other programming languages like AutoHotkey which do not require you to specify data type at the time of creating variables like Python, PHP, Perl, etc.,
- You can give nearly any name to a variable like age, sex, salary, year1990 or anything else you like to give, AutoHotkey allows you to use only limited characters in variables names and has some Reserved keywords. For now, I will suggest to use only a….z, A….Z, 0….9, _ in your variable names and start their names using alphabets only instead of digits.
- Not many programming languages allows variable names to start with a digit, so 1990year will not be a valid variable name when not using AHK where as year1990 or ye1990ar are valid variable names in most cases.
Every programming language provides even more rules related to variables and you can learn them if you go in further detail of that said programming language.
Store Values in Variables
You have seen how to create an empty variable and shortly how to store other things in the previous section.
Now, let’s store the two values from above in the variables −
a := 20 b := 10
This script has two statements where we are storing 20 in variable a and 10 is being stored in variable b.
Almost all the programming languages have similar ways of storing values in variable
Where we keep variables name in the left hand side of an equal sign :=
and whatever value we want to store in the variable:
we keep in the right hand side.
Now, we have completed two steps in one, we created two variables and we stored required values in those variables.
Now variable a has value 20 and variable b has value 10.
In other words we can say that the program knows that when the above script is executed, the memory location named a will hold 20 and memory location b will hold 10.
Variables: Access values stored in them
If we do not use the stored values in the variables, then there is no point in creating variables and storing values in them.
We know that the above program has two variables a and b and they store the values 20 and 10, respectively.
So let’s show the values stored in these two variables.
The following is an AHK Script which shows the values stored in its variables −
a := 20 b := 10 MsgBox, Value of a = %a% MsgBox, Value of b = %b%
When the above program is executed, it produces two message boxes one after the other with the following content −
Value of a = 20 Value of b = 10
We are using the MsgBox command to show the values of variables.
We are making use of %a%, which will be replaced with the values of the given variable in the statement.
We can show both the values using a single msgbox statement as follows −
a := 20 b := 10 MsgBox, Value of a = %a% and Value of b = %b%
When the above script is executed, it produces the following result −
Value of a = 10 and value of b = 20
Using AutoHotkey as a Calculator
Let’s try some simple AutoHotkey math.
You can type an expression and store its value.
Expression syntax is straightforward:
The operators +, -, * and / work just like in most other languages
value := 2 + 2 MsgBox, Value of 2 + 2 is %value%
Parentheses () can be used for grouping. For example:
value := (50 - 5.0*6) / 4 MsgBox, Value of the expression is %value% ; aka 5
The colon equal sign (:=) is used In AutoHotkey to assign a value to a variable and the normal equal sign is used to compare values.
Afterwards, the result in the variable(s) can be displayed or used for the next expression or script action.
That’s it for variables, what’s your take on it did I miss something?
[…] So far, we have covered an important concept called variables. […]