When you add an element to a list, the list remembers automatically what data type the value was.

Generally, because you're the one adding something to a list, you know what datatype is in what place in the list, and you retrieve it out of the list with the appropriate llList2<type> function such as: llList2String, llList2Vector, etc. (more on this later.)

If for some reason, though, you need to test what data type an element is in a list, you can use the llGetListEntryType function.

Tip! When adding a float to a list, always add it with a decimal point (e.g 1.0 as opposed to 1) to ensure that it is preserved as a float.

List Limits

While a script is running, a list can grow dynamically as large as needed, limited only by the amount of memory that is available in the script.

However, at compile (aka save) time, there is a 72 element limit to pre-defined lists hardcoded in the script. Such long, predefined lists are common, for example, when someone is offering the user a plethora of colour choices.

Tip! If you really need 72 or more such choices in a pre-defined list, just make 2 (or more) lists that don't upset the compiler, and join them together in state_entry() or wherever appropriate:

<lsl>biggerlist = biglist01 + biglist02;</lsl>

Adding an Element to a list

There are several ways used to add an element to an existing list via prepending/appending:

myList = [new_item] + myList; Best method for Mono-LSL

myList = myList + [new_item];

myList += [new_item];

myList = (myList=[]) + myList + [new_item];

myList = myList + new_item;

myList += new_item;

myList = (myList=[]) + myList + new_item; Best method for LSO-LSL

Notes

As of 8/8/2009 Method 1 gives significant savings over any other method used in Mono-LSL.

Note that prepending the new_item without brackets negates any memory savings in Mono-LSL.

Method 1 will consume more memory than other methods in LSO-LSL.

As of 8/8/2009 Method 7 returns the best value for savings in LSO-LSL.

Method 7 is better than Method 4 due to the fact that Method 7 takes up less script overhead.

Method 7 test script compiled to have a start memory of 15878, while Method 4 compiled to have a start memory of 15871. As a result Method 7 had more free memory than Method 4 at the end of the test operation.

Methods 2 & 3 compile to the same thing.

Methods 5 & 6 compile to the same thing.

Methods 5, 6 & 7 have a bytecode savings over methods 2, 3 & 4 respectively, though there is an LSO-LSL VM bug that causes the string & key typecasts to not stick properly: SVC-1710.

Methods 4 & 7 can result in a considerable memory savings in LSO-LSL over methods 2, 3, 5 & 6 (it helps reduce heap fragmentation, which would otherwise result in unusable blocks of heap memory)[1]. In Mono-LSL it provides no significant memory advantage or disadvantage.

Depending upon the situation (in LSO-LSL) this method may not provide any advantage what so ever. If in doubt profile the script with and without using this method.

Joining Lists (aka Concatenation)

Lists can be joined simply by using the + sign:

<lsl>newlist = list01 + list02;

newlist = list01 + ["red","brown",<0,0,0>];</lsl>

Note: The above example actually creates 3 lists in memory while the command runs, even though just one is returned. This can affect memory usage.

Clearing a List

To clear a list, set it equal to two square, empty brackets like this:

<lsl>myList = [];</lsl>

Strided lists

One common use of lists is to duplicate the functionality of structured collections of data (aka structs). Such collections, available in many programming languages, are absent from LSL.

In-world in SL, (still as of July 2008), a strided list is the closest you can get to storing limited amounts of data in some kind of structure that you can access and manipulate in a few, limited ways.

Strided lists allow you to store related data pieces grouped (aka "strided") in sets. You can determine how many pieces of data in each "grouping."

An example is best at this point. You might use a strided list to track the names, gender and rez dates of a group of avatars:

This example has a stride of three, because each grouping (or stride) has three data elements. Index 0 ("John Adams") is the start of the first instance (because list counting starts a 0), index 3 ("Shirley Bassey") is the start of the second instance, and so on.

It is important that the pieces of information in each grouping are always entered in the same sequence for every instance of the grouping in the list! In the example above, this means that the name needs to be always the first of the three related elements. You should consider carefully the order in which you record information because the function to sort a list, llListSort, will only sort on the first element of the instances. In other words, if the avatar's rez date were the most important attribute for your script, then you would need to record it first, and the name second. If you wish to be able to sort by avatar last name rather than first name, the name should be added to the list in Lastname FirstName format (avoiding a comma, though, of course, unless you wanted LastName and FirstName treated as separate elements in the list.)

To add another person to the above list, you would do this:

<lsl>demographics += ["Dorthy Lamour", "female", "2010-01-22"];</lsl>

You cannot do any kind of fancy data analysis or manipulation with strided lists, as you can in off-world databases or spreadsheets, but they can meet some limited, in-world needs, if you don't have the resources to tie-in off-world tools.

Here are the tools we do have for strided lists:

The following three (as of July 2008) native LSL functions can be used with strided lists:

Returns a new list, composed of elements that were in either original script, but not both. Note: This is not the same as getting rid duplicates by preserving just one of each duplicated item. It goes further, and removes both items.