Lists (old tutorial)

Lists (old tutorial)

> Tutorial

There is another type of variable that we briefly mentioned earlier: lists.

So far, the variables we have seen all have a name and a value. Lists also have a name, but you can associate several values with them; these values are then stored in "slots".

Lists in LeekScript use numeric indexes (or keys). You can store any type of value in them (including another list), and each slot can even hold a different type.

The first index of a list is always 0, and negative indexes cannot be used.

Creating a list

To create a list, use the following syntax:

// Declare an empty list var myList = []

You can fill your list in two ways:

In both cases, the resulting list can be represented like this:

Index | Value -------|------- 0 | Leek 1 | Llama 2 | Ostrich 3 | Artichoke

Reading a list

You can access a slot of a list by putting the index of that slot between square brackets:

var myList = ["Leek", "Llama", "Ostrich", "Artichoke"] debug(myList[1]) // Displays Llama (the first element is at index 0)

You can use a slot of a list exactly like a normal variable:

var myList = [2, 4, 6, 8] debug(myList[0]) // Displays 2 myList[0] = myList[1] + 5 // Assigns the value 9 to slot 0 debug(myList[0]) // Displays 9