Difference between revisions of "LlGetListLength"

From Second Life Wiki
Jump to navigation Jump to search
m
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{LSL_Function
{{Issues/SVC-6364}}{{LSL_Function
|func_id=185|func_sleep=0.0|func_energy=10.0
|func_id=185|func_sleep=0.0|func_energy=10.0
|func=llGetListLength|return_type=integer|p1_type=list|p1_name=src
|func=llGetListLength|return_type=integer|p1_type=list|p1_name=src
|func_footnote
|func_footnote
|func_desc
|func_desc
|return_text=that is the number of elements in the list '''src'''.
|return_text=that is the number of elements in the list {{LSLP|src}}.
|spec
|spec
|caveats=
|caveats=
|constants
|examples=
<source lang="lsl2">
//Basic usage
default
{
    state_entry()
    {
        list testList = ["one", "two", "three"];
        integer length = llGetListLength(testList);
        llOwnerSay("There are " + (string)length + " entries in the list.");
    }
}
</source>
|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:


<lsl>
<source lang="lsl2">
integer i;
integer length = llGetListLength(mylist);
integer length = llGetListLength(mylist);
for (i = 0; i < length; ++i) {
    llSay(0, llList2String(mylist, i));
}
</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.)
integer index;// default is 0
 
while (index < length)
<lsl>
{
integer i;
     llSay(0, llList2String(mylist, index));
for (i = 0; i < llGetListLength(mylist); ++i) {
    ++index;
     llSay(0, llList2String(mylist, i));
}
}
</lsl>
</source>


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{{Footnote|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).}}.


|constants
<source lang="lsl2">
|examples=
integer index;// default is 0
<lsl>
while (index < llGetListLength(mylist))
default
{
{
     state_entry()
     llSay(0, llList2String(mylist, index));
    {
    ++index;
        list l = ["one", "two", "three"];
        integer i = llGetListLength(l);
        llOwnerSay("there are " + (string)i + " entries in the list");
    }
}
}
</lsl>
</source>
|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 Tip|Please read [[User:Kireji_Haiku/How_to_deal_with_lists_in_LSL|this intro of how to iterate over a list in LSL]].}}
<lsl>
 
===LSO 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, meaning that it returns the same result as <code>llGetListLength()</code>, 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.
<source lang="lsl2">
list in;
list in;
integer len_in = llGetListLength(in);
integer len_in = llGetListLength(in);
Line 50: Line 60:
integer fneg_len_in = ([] != in);
integer fneg_len_in = ([] != in);
//fneg_len_in and neg_len_in will be the same
//fneg_len_in and neg_len_in will be the same
</lsl>
</source>
|helpers
|helpers
|also_functions=
|also_functions=

Latest revision as of 01:10, 22 January 2015

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

Examples

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

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:

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 loop[1].

integer index;// default is 0
while (index < llGetListLength(mylist))
{
    llSay(0, llList2String(mylist, index));
    ++index;
}
KBcaution.png Important: Please read this intro of how to iterate over a list in LSL.

LSO 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, 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

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

Footnotes

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

Signature

function integer llGetListLength( list src );