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; 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] results in the same list as [5], and (string)[3.7, 8.3] results in the string 3.7000008.300000. No other type casts are allowed for lists.

The + operator is the only one that list supports, and 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].