Skip to content
lsl.devlsl.devLSL Dev

llGetListLength

function
Function syntax
integer llGetListLength(
  1. list src// List to measure.
);
Returns
integer
Energy
10

Returns an integer representing the total number of elements in the list src.

Returns an integer that is the number of elements in the list src.

How to use
integer result = llGetListLength([]);
Example 1
//Basic usage
default
{
state_entry()
{
list testList = ["one", "two", "three"];
integer length = llGetListLength(testList);
llOwnerSay("There are " + (string)length + " entries in the list.");
}
}

When using list length to help you loop through a list, it is better to determine the length first, then start your loop:

integer length = llGetListLength(mylist);
integer index;// default is 0
while (index < length)
{
llSay(0, llList2String(mylist, index));
++index;
}

The following example is to illustrate what not to do, it calculates the length in the “for” loop and is inefficient because the length gets recalculated at each pass through the loop. This should only ever be done if the list is in fact changing (in length) with each iteration of the loop1.

integer index;// default is 0
while (index < llGetListLength(mylist))
{
llSay(0, llList2String(mylist, index));
++index;
}

A faster and lighter (in bytecode) way to determine the length of a list is to do a not-equals compare with a null list. This works because the list not-equals compare returns the difference between the lengths, meaning that it returns the same result as llGetListLength(), minus the overhead in bytecode, and performance penalty of calling a non-native function. Note: This optimization is much less beneficial time-wise in Mono as Mono’s llGetListLength function is almost twice as fast, however the bytecode saving is still about 30 bytes.

list in;
integer len_in = llGetListLength(in);
integer flen_in = (in != []);
//flen_in and len_in will be the same
integer neg_len_in = -llGetListLength(in);
integer fneg_len_in = ([] != in);
//fneg_len_in and neg_len_in will be the same
Original wiki source

Some wiki templates and tables need their original context. View this article on the Second Life Wiki. Technical wording and examples are retained from the source; historical guidance may differ from current behavior.

From the Second Life Wiki

Community documentation adapted from LlGetListLength, by Linden Research, Inc. and contributing residents.View contributors and history.

Imported wiki revision:

Licensed under CC BY-SA 3.0. Last wiki edit: .Formatting and links have been adapted for this site; API metadata follows the LSL definitions.

  1. Recalculating the length is slow because the VM duplicates the entire list (including the values) when it is pushed on the stack (so it can be popped off the stack when the length is calculated). ↩