Don't click here unless you want to be banned.

LSL Wiki : llGetListLength

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are
integer llGetListLength(list src)

Returns the number of elements in the list src.

list foo = ["bar", "baz"];
llOwnerSay((string) llGetListLength(foo));

This example will make the object say "2" to the object owner.

llGetListLength returns the number of elements in a list, not the maximum index in a list. To loop through all the elements in a list, use this:

list MyList = ["a","b","c"];
integer length = llGetListLength(MyList);
integer i;
for (i = 0; i < length; ++i) {
    llOwnerSay(llList2String(MyList, i));

Note: In the above example, the length integer is declared, calling llGetListLength a single time, rather than calling it within the for loop. As this forum thread discusses, doing it this way results in a much faster loop.

list MyList = ["a","b","c"];
integer length_slow = llGetListLength(MyList); //Slow
integer length_fast = (MyList != []); //Fast
integer neg_length_fast = ([] != MyList); //Fast & negative

This article wasn't helpful for you? Maybe the related article at the LSL Portal is able to bring enlightenment.

Functions | List
Comments [Hide comments/form]
Preserving cruft:

Note: "Thrifty" (clever) scripters may wish to conserve script memory when attempting to determine if a list is empty by trying:
if (myList == [])
...rather than:
if (llGetListLength(myList) == 0)
Surprisingly, llGetListLength is actually more efficient. Try it for yourself.-?
It shouldn't be more efficient, it's has a greater bytecode demand- BW
Are you sure? I think comparing lists might require more processing power than merely extracting the size of a list -- KeknehvPsaltery
LSL doesn't compair list data, only thier lengths. [1] == [2], [1] == ["a"] both returns TRUE- BW
I think the function would be more efficient as its pulling just data about the list rather than loading the list to get its length and compare to an empty list -- GrazelCosmo
The function call causes the list to be duplicated in memory, causing increased memory usage despite possibly being faster to actually run. The comparison avoids duplicating the list on the heap, reducing memory load. Function Call = faster, Comparison Annoyance Exploitation = smaller memory load. -- WW
-- ReadyJack (2006-03-01 13:06:14)
-- (2007-09-16 14:34:54)
Attach a comment to this page: