Difference between revisions of "LlGetListLength"

From Second Life Wiki
Jump to navigation Jump to search
m
m
Line 7: Line 7:
|spec
|spec
|caveats=
|caveats=
*The LSL compiler will not optimize the condition, it will not hoisting this function out of the condition. Unless ''strictly'' necessary all work with strings, keys and lists should be manually hoisted and performed outside of the loop. See [[#Best_Practices|Best Practices]] for more details.
|constants
|examples=
<lsl>//Basic usage
default
{
    state_entry()
    {
        list l = ["one", "two", "three"];
        integer i = llGetListLength(l);
        llOwnerSay("there are " + (string)i + " entries in the list");
    }
}</lsl>
|notes=
===Best Practices===
When using list length to help you loop through a list, it is better to determine the length first, then start your loop:
When using list length to help you loop through a list, it is better to determine the length first, then start your loop:


Line 17: Line 33:
</lsl>
</lsl>


The example below which calculates length in the "for" loop is inefficient because length gets recalculated at each pass through the loop (so, to be clear, do it the above way instead.)
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 loop.


<lsl>
<lsl>
Line 26: Line 42:
</lsl>
</lsl>


 
===Optimizations===
|constants
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.
|examples=
<lsl>
default
{
    state_entry()
    {
        list l = ["one", "two", "three"];
        integer i = llGetListLength(l);
        llOwnerSay("there are " + (string)i + " entries in the list");
    }
}
</lsl>
|notes=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.
<lsl>
<lsl>
list in;
list in;

Revision as of 13:18, 14 July 2008

Summary

Function: integer llGetListLength( list src );
0.0 Forced Delay
10.0 Energy

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

• list src

Caveats

  • The LSL compiler will not optimize the condition, it will not hoisting this function out of the condition. Unless strictly necessary all work with strings, keys and lists should be manually hoisted and performed outside of the loop. See Best Practices for more details.

Examples

<lsl>//Basic usage default {

   state_entry()
   {
       list l = ["one", "two", "three"];
       integer i = llGetListLength(l);
       llOwnerSay("there are " + (string)i + " entries in the list");
   }
}</lsl>

Notes

Best Practices

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

<lsl> integer i = 0; integer length = llGetListLength(mylist); for (; i < length; ++i) {

   llSay(0, llList2String(mylist, i));

} </lsl>

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 loop.

<lsl> integer i; for (i = 0; i < llGetListLength(mylist); ++i) {

   llSay(0, llList2String(mylist, i));

} </lsl>

Optimizations

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. <lsl> 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 </lsl>

See Also

Functions

•  llListStatistics LIST_STAT_NUM_COUNT – Returns the number of integers and floats in the list
•  llStringLength Returns the number of characters in a string.

Deep Notes

Source

'linden\indra\lscript\lscript_library\lscript_alloc.cpp' @ lsa_cmp_lists
'linden\indra\lscript\lscript_execute\lscript_execute.cpp' @ list_list_operation

Signature

function integer llGetListLength( list src );