Variables - Jamiras/RATools GitHub Wiki

Variables allow storage of values for for later use. They may be assigned constant or calculated values.

Variables within a function (including function parameters) are only accessible within the function. Variables outside a function may be used inside or outside of functions.

Variables may not be referenced before they are defined.

Assignment

  • name = value

name must start with a letter or underscore and may contain additional letters, numbers or underscores.

value may be a numerical constant, string constant, or expression. The expression will not be evaluated until the variable is used, which allows variables to be assigned to function calls, memory accessors, or logical comparisons to be evaluated later.

Arrays

  • name = [ value1, value2 ]

An array is a variable that stores multiple values. Arrays are accessed using square brackets. Indices are zero-based, so to access the first item of the array, use name[0].

  • array_push(array, value)

Appends value to the end of the array.

  • array_pop(array)

Removes and returns the last value from an array. If the array is empty, integer 0 is returned. You can use length() to determine if the array is empty before calling array_pop.

Example

my_array = [1, 3, 8, 15]
array_push(my_array, 21)

for i in my_array
    do_something(i)

This creates an array with four elements, then adds another element. Later, the array is iterated and a helper function is called five times (once for each element in the array).

Dictionaries

  • name = { key: value, key: value }

A dictionary is a special type of variable that maps integer values to other values - typically string constants. Anything that can be assigned to a variable can be stored in the value of a dictionary entry.

key must be a numerical or string constant or a variable or function call that evaluates to a numerical or string constant. It will be evaluated when the dictionary is being built.

Dictionaries can be accessed or modified using square brackets: value = name[key]. name[key] = value

Example

two = 2
my_dict = {
   1: "One",
   two: "Two"
}

my_dict[3] = "Three"

for key in my_dict:
   do_something(key, my_dict[key])

This creates a dictionary with two entries, then adds another entry. Later, the dictionary is iterated and a helper function is called three times (once for each entry in the dictionary).