Arrays

An array is an ordered list of values.

Create an array#

To create an array, use square brackets [].

Empty array#

list = []

Array with numbers#

numbers = [1 2 3 4 5]

Array with text#

fruits = ["apple" "banana" "cherry"]

Mixed array#

You can mix different types of values:

mixed = [10 "text" true 3.14]

Matrix (array of arrays)#

An array can contain other arrays:

matrix = [
  [1 2 3]
  [4 5 6]
  [7 8 9]
]

Read array elements#

To access an element, use its index between square brackets [].

fruits = ["apple" "banana" "cherry"]

print fruits[0]  # apple (first element)
print fruits[1]  # banana (second element)
print fruits[2]  # cherry (third element)
Indices

The first element is always at index 0.

Modify elements#

To change a value, assign a new value using its index:

list = [1 2 3]
list[0] = 100
print list   # [100 2 3]
Length

The len property indicates how many elements are in the array. Methods like push or pop to add and remove elements are on the Array methods page.