6.2. List Values

List values are used to hold a list of other values. To create a list you use the ‘List()’ function. It takes one argument that tells how many elements it contains. If it is left out, the created list will be empty. Use the get and set operations to retrieve and set elements.

        .var myList = List(2)
        .eval myList.set(0,25)
        .eval myList.set(1, "Hello world")
        .byte myList.get(0)    // Will give .byte 25
        .text myList.get(1)    // Will give .text "Hello world"

You can determine the number of elements in a list with the size-function and the add-function adds additional elements.

        .var greetingsList = List()
        .eval greetingsList.add("Fairlight", "Booze Design", "etc." )
        .byte listSize = greetingsList.size()    // gives .byte 3

A compact way to fill a list with elements is:

        .var greetingsList = List().add("Fairlight", "Booze Design", "etc.")

Here is a list of functions defined on list values:

Table 6.2. List Values

Functions Description
get(n) Gets the n’th element (first element starts at zero).
set(n,value) Sets the n’th element (first element starts at zero).
add(value1, value2, …) Add elements to the end of the list.
addAll(list) Add all elements from another list.
size() Returns the size of the list.
remove(n) Removes the n’th element.
shuffle() Puts the elements of the list in random order.
reverse() Puts the elements of the list in reverse order.
sort() Sorts the elements of the list (only numeric values are supported).