Unofficial LSL Reference

[[types:list]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

This is an old revision of the document!


List type

A list is a sequentially numbered collection of elements of other types. It's the closest thing LSL has to what other languages call an array. Lists can have any number of elements, from zero to as many as available memory can hold. You can think of a list as a row of a spreadsheet in which each cell has an element that can be one of the LSL types, but instead of having the columns labelled "A", "B", and so on, they are numbered starting from zero. We refer to that numbering as the index or position of the element.

List literals are written in LSL as a comma-separated sequence enclosed within brackets. An empty list has nothing within the brackets, so an empty list literal is written like this: []

Lists can not contain lists, but they can contain elements of any other type LSL supports. For example, [3, "yo!", <5, 1.4, -8>, PI] is a valid list literal. In it, the element 3 is the one at position 0, the element "yo!" is at position 1, the element <5, 1.4, -8> is at position 2, and the element PI is at position 3. Or we can also say that the index of the element "yo!" is 1.

Note that there is no way to specify key literals, therefore if a key element is needed in a list, it has to come from a type cast, from a function, or from a variable of type key, and only the latter is allowed in the globals section. See the key type reference for details.

Individual list elements are accessed through functions. The following functions can be used to access individual elements of a list: llList2Integer, llList2Float, llList2String, llList2Key, llList2Vector, and llList2Rot. Each of them requires two arguments: the list to extract the element from, and the index of the element to extract. If the index is out of range, no error is reported; instead, a default value for the corresponding type is returned (zero for integers and floats, empty string for strings, empty or null key for keys, ZERO_VECTOR for floats, and ZERO_ROTATION for rotations). See the reference of each function for details.

One quirk of LSL is that list variables can not be altered: in order to alter a list, you need to create a new list with the new contents. There are a number of functions to assist you with that, including llListReplaceList, llDeleteSubList, llList2List and so on. The types of the individual elements of a list can be retrieved using llGetListEntryType. See list functions for a complete reference.

Type cast of any type other than list to list results in a list of one single element. Type cast of a list to list does nothing to it. Type cast of a list to string results in a string that has every element of the list cast to string, and then all of them concatenated together, as if llDumpList2String was used with the list and an empty string as arguments. For example, (list)3 results in the same list as [3]; (list)"hi" results in the same list as ["hi"]; (list)[5, 4] results in the same list as [5, 4], and (string)[3.7, 8.3] results in the string 3.7000008.300000. No other type casts are allowed for lists.

Joining lists

The + operator returns a new list which results from joining both lists (if one of the sides of the operator is not a list, it is converted to list first). Items in the second list are renumbered so that the numbers remain sequential.

For example, ["a", "b"] + ["c", "d"] results in the same list as ["a", "b", "c", "d"]. Note that the element "c" is initially at position 0 of the second list, but in the final list it is at position 2.

Another example: both [2] + 3 and 2 + [3] result in the same list as [2, 3]. Note how, since one of the elements is of type list, the other is automatically cast to list before joining the lists. Note also that [2] + 3 + 4 is not the same as [2] + (3 + 4): the former is evaluated left-to-right, so it first sees [2] + 3 and results in [2, 3], then it sees [2, 3] + 4 and results in [2, 3, 4], while the latter first evaluates the expression within parentheses which does not contain lists and therefore is taken as integer addition, resulting in 7, then it sees [2] + 7 and results in [2, 7].

Comparing lists

The != operator can be used on two lists. Counter-intuitively, the result is an integer that is the subtraction of the lengths of the lists. For example, [5] != [6, 2, "blah"] returns -2, because the length of the first list is 1 and the length of the second list is 3, and 1 - 3 equals -2. This quirk can be exploited to use != as an alternative to llGetListLength to make the code take less space, but keep in mind that it is not portable to other grids.

The == operator can also be used, and it compares the lengths of the lists. If the lengths are equal, it returns TRUE; otherwise it returns FALSE. For example, [5, 2] == ["hello", "world"] returns TRUE.

Both operators can be safely used to compare a list to the empty list. Note, however, that the != one may return values other than TRUE and FALSE.

Examples

The following is a basic example that holds a list of all the people who touched it. If touched by the owner, it lists the people who have touched it so far. For simplicity, it has no clean-up or memory overflow guard, therefore it can result in a stack-heap collision error if many people touch it.

list-type-example.lsl
list Touchers;

default
{
    touch_start(integer n)
    {
        if (llDetectedKey(0) == llGetOwner())
        {
            integer length = llGetListLength(Touchers);
            integer index;
            // Loop through every index in the list and display the element
            for (index = 0; index < length; ++index)
                 llOwnerSay(llList2String(Touchers, index));
        }
        else
        {
            // Check if this name is already present in the list
            if (llListFindList(Touchers, (list)llDetectedName(0)) == -1)
                // Not present, add it.
                Touchers += llDetectedName(0);
        }
    }
}