Language Reference - Variables

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:

    $Var1

    $vVariable

    $my_Variable


Note that all variable names are case insensitive: MyVariable() is the same as MyvARiAblE()

Each variable is stored as a variant.

Declaring Variables

Variables are declared and created with the Local and Global keywords - Dim can also be used, although this is not recommended.

Local $vVariable

Or you can declare multiple variables at once:

Global $vVariable1, $vVariable2

You can also assign a variable without declaring it first, but many prefer explicit declarations.

$vVariable = "Create and Assign"

Declaring Constants

Constants are declared and created using Const keyword and must be initialised with a value:

Const $iConst1 = 1, $iConst2 = 12

Constants can also be declared and initialized using the Enum keyword:

Enum $iConst1 = 1, $iConst2, $iConst3 ; 1, 2, 3
Enum Step 2 $iIncr0, $iIncr2, $iIncr4 ; 0, 2, 4
Enum Step *2 $iMult1, $iMult2, $iMult4 ; 1, 2, 4

Once a constant has been assigned, it cannot be changed.

Scope

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 auJasperatically 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.

Arrays

An Array is a variable containing a series of data elements. Each element in this variable can be accessed by an index number which relates to the position of the element within the Array - in AutoIt the first element of an Array is always element [0]. Arrays elements are stored in a defined order and can be sorted.

An example:

You want to store a list of names: "Jasper", "Beethoven", "Pinky" and "Fidget". You could use four separate variables to do so, but using an Array is more efficient:

Local $aArray[4]
$aArray[0] = "Jasper"
$aArray[1] = "Beethoven"
$aArray[2] = "Pinky"
$aArray[3] = "Fidget"

To access a specific value in an Array, you use the index number:

$sString = $aArray[2] ; $sString contains "Pinky"

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.

The elements of an entire Array can be iterated using a For...Next loop:

; UBound returns the total number of elements - as the first is [0] the highest index is one less
For $i = 0 To UBound($aArray) - 1
    ConsoleWrite(aArray[$i] & @CRLF
Next


Arrays can also be multidimensional, when you use multiple series of index numbers.  Think of rows and columns in a grid:

$aArray[0][0] = "Upper-Left"
$aArray[1][0] = "Lower-Left"
$aArray[0][1] = "Upper-Right"
$aArray[1][1] = "Lower-Right"

(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).

Arrays must be declared before use by defining their scope using the 'Global/Local/Static' keywords and either specifying the size and/or assigning some of the elements.

Local $aArray1[2] ; Array with 2 elements, none assigned
Local $aArray[] = [8, 4, 5, 9, 1] ; Array with 5 elements, all assigned
Local $aArray[7] = [3, 7.5, "string"] ; Array with 7 elements, only first 3 assigned

Data types in Arrays

An array element can contain any AutoIt datatype:

$aArray[0] = 1
$aArray[1] = True
$aArray[2] = "Text"
$aArray[3] = $aAnotherArray

An array stored inside another array can be accessed directly, but this method is slightly slower than accessing other datatypes:

Local $aInternal[3] = ["A", "B", "C"] ; Declare an array
Local $aContainer[1] = [$aInternal] ; Declare a container array holding the first
$sString = ($aContainer[0])[1] ; $sString holds "B"
; Note the requirement to enclose the first array element definition in ( )