A variable is just a place to store data in memory so that it can be accessed quickly. Think of it as a mailbox in memory that you can put information in or take information out of. For example you might create a variable to store the number a user's response to a question, or the result to a math equation.
Each variable has a name (again, similar to a mailbox) and must start with the $ character and may only contain letters, numbers and the underscore _ character. Here are some example names:
Each variable is stored as a variant.
Variables are declared and created with the Dim, Local and Global keywords:
Or you can declare multiple variables at once:
Dim $var1, $myvariable
You can also assign a variable without declaring it first, but many prefer explicit declarations.
$var1 = "create and assign"
Constants are declared and created using Const keyword like:
Const $const1 = 1, $const2=12
Constants can be declared and initialized using Enum keyword like:
Enum $const1 = 1, $const2, $const3
; 1, 2, 3
Enum STEP 2 $incr0, $incr2, $incr4 ; 0, 2, 4
Enum STEP *2 $mult1, $mult2, $mult4 ; 1, 2, 4
Constants cannot redeclare an existing variable.
A variable's scope is controlled by when and how you declare the variable. If you declare a variable at the start of your script and outside any functions it exists in the Global scope and can be read or changed from anywhere in the script.
If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.
By default when variables are declared using Dim or assigned in a function they have Local scope unless there is a global variable of the same name (in which case the global variable is reused). This can be altered by using the Local and Global keywords to declare variables and force the scope you want.
An Array is a variable containing series of data elements of the same
type and size. Each element in this variable can be accessed by an index number.
Let's say you want to store these series of characters: "A", "U", "T", "O", "I", "T" and "3".
You could use seven separate variables to do so, but using an Array is more efficient:
To access a specific value in an Array, you only have to know the index number:
This results in $MyChar containing the letter "T" (See also: 'operators').
The index number can also be substituted by another variable or an expression, so you can build complex ways to assign or access elements in an array.
Arrays can also be multi dimensional, when you use multiple series of index numbers, like:
(These values are just examples)
You can use up to 64 dimensions in an Array. The total number of entries cannot be greater than 2^24 (16 777 216).
Before you can start using Arrays in your script, you must define their bounds using the 'Dim' keyword.
It was said that an Array contains only one datatype of the same type. But technically speaking, a Variant in AutoIt can contain anything from a number to a boolean value. So an AutoIt-Array could also contain different types, even other Arrays:
This has not been strictly forbidden in AutoIt. However, it is NOT ADVISABLE to mix different datatypes in an Array. Especially the use of an Array inside another Array will severely affect the execution speed of your script.