Difference between revisions of "User:Kireji Haiku/How to deal with lists in LSL"

From Second Life Wiki
Jump to navigation Jump to search
(new page)
 
m (added code for when indexes cannot be negative)
Line 76: Line 76:
Because ++index is faster than index++ we could start with -11 and increment first before doing something, instead of the other way around before... as long as the index stays negative. We can write:
Because ++index is faster than index++ we could start with -11 and increment first before doing something, instead of the other way around before... as long as the index stays negative. We can write:


<lsl>
{| class="sortable" {{Prettytable}}
|- {{Hl2}}
! '''if indexes can be negative'''
! '''if indexes can NOT be negative'''
|-
||<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer lengthOfList = llGetListLength(listOfTenItems);
Line 86: Line 91:
}
}
</lsl>
</lsl>
||
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer index = -1;
for (;index < lengthOfList;++index)
{
    //do something
}
</lsl>
|}


Now trying to simplify the math for calculating the index default value we can use '''bitwise-NOT of the length''' which is the same as the negative length minus one:
Now trying to simplify the math for calculating the index default value we can use '''bitwise-NOT of the length''' which is the same as the negative length minus one:


<lsl>
{| class="sortable" {{Prettytable}}
|- {{Hl2}}
! '''if indexes can be negative'''
! '''if indexes can NOT be negative'''
|-
||<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer lengthOfList = llGetListLength(listOfTenItems);
Line 100: Line 122:
}
}
</lsl>
</lsl>
||
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer index = -1;
for (;index < lengthOfList;++index)
{
    //do something
}
</lsl>
|}


as you might have seen by now, we can merge two lines in the middle:
as you might have seen by now, we can merge two lines in the middle:


<lsl>
{| class="sortable" {{Prettytable}}
|- {{Hl2}}
! '''if indexes can be negative'''
! '''if indexes can NOT be negative'''
|-
||<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];


Line 113: Line 152:
}
}
</lsl>
</lsl>
||
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer index = -1;
for (;index < lengthOfList;++index)
{
    //do something
}
</lsl>
|}


and because index should have a negative value while looping and the loop stops when index reaches zero, we can change the code to:
and because index should have a negative value while looping and the loop stops when index reaches zero, we can change the code to:
 
{| class="sortable" {{Prettytable}}
<lsl>
|- {{Hl2}}
! '''if indexes can be negative'''
! '''if indexes can NOT be negative'''
|-
||<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];


Line 126: Line 181:
}
}
</lsl>
</lsl>
||
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer index = -1;
for (;index < lengthOfList;++index)
{
    //do something
}
</lsl>
|}


and improve readability by using a while-loop:
and improve readability by using a while-loop:


<lsl>
{| class="sortable" {{Prettytable}}
|- {{Hl2}}
! '''if indexes can be negative'''
! '''if indexes can NOT be negative'''
|-
||<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];


Line 139: Line 211:
}
}
</lsl>
</lsl>
||
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer index = -1;
while (++index < lengthOfList)
{
    //do something
}
</lsl>
|}
</div>
</div>
</div>
</div>
Line 145: Line 229:
<div style="padding: 0.5em">
<div style="padding: 0.5em">
and try to get rid of the bitwise-NOT because some people find it hard to read. The positive side effect of the next step is that you need to increment one step less in total and the loop runs the first step directly without checking first for a conditional statement:
and try to get rid of the bitwise-NOT because some people find it hard to read. The positive side effect of the next step is that you need to increment one step less in total and the loop runs the first step directly without checking first for a conditional statement:
<lsl>
{| class="sortable" {{Prettytable}}
|- {{Hl2}}
! '''if indexes can be negative'''
! '''if indexes can NOT be negative'''
|-
||<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];


Line 157: Line 246:
while (++index);
while (++index);
</lsl>
</lsl>
||
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);
integer index;
do
{
    //do something
}
while (++index < lengthOfList);
</lsl>
|}
</div>
</div>
</div>
</div>

Revision as of 10:27, 6 November 2012

How to deal with lists in the Linden Scripting Language:

the basics

Usual list with 10 items:

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; </lsl>

Then start with a for-loop:

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

integer index = 0; for (index = 0;index < llGetListLength(listOfTenItems);index++) {

   //do something

} </lsl>

Resetting the value to 0 for index was redundant so we can skip that:

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

integer index = 0; for (;index < llGetListLength(listOfTenItems);index++) {

   //do something

} </lsl>

Setting the default value to 0 is redundant, too.

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

integer index; for (;index < llGetListLength(listOfTenItems);index++) {

   //do something

} </lsl>


Calculating the list length every step is redundant too because the length doesn't change.

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index; for (;index < lengthOfList;index++) {

   //do something

} </lsl>

slightly more advanced now

Now the index for items in a list of ten is either:

 0,  1,  2,  3,  4,  5,  6,  7,  8,  9

//  or

-10, -9, -8, -7, -6, -5, -4, -3, -2, -1

Because ++index is faster than index++ we could start with -11 and increment first before doing something, instead of the other way around before... as long as the index stays negative. We can write:

if indexes can be negative if indexes can NOT be negative
<lsl>

list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index = -lengthOfList - 1; for (;index < 0;++index) {

   //do something

} </lsl>

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index = -1; for (;index < lengthOfList;++index) {

   //do something

} </lsl>

Now trying to simplify the math for calculating the index default value we can use bitwise-NOT of the length which is the same as the negative length minus one:

if indexes can be negative if indexes can NOT be negative
<lsl>

list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

// it's bitwise-NOT (~) not minus (-) integer index = ~lengthOfList; for (;index < 0;++index) {

   //do something

} </lsl>

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index = -1; for (;index < lengthOfList;++index) {

   //do something

} </lsl>

as you might have seen by now, we can merge two lines in the middle:

if indexes can be negative if indexes can NOT be negative
<lsl>

list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

// it's bitwise-NOT (~) not minus (-) integer index = ~llGetListLength(listOfTenItems); for (;index < 0;++index) {

   //do something

} </lsl>

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index = -1; for (;index < lengthOfList;++index) {

   //do something

} </lsl>

and because index should have a negative value while looping and the loop stops when index reaches zero, we can change the code to:

if indexes can be negative if indexes can NOT be negative
<lsl>

list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

// it's bitwise-NOT (~) not minus (-) integer index = ~llGetListLength(listOfTenItems); for (;index;++index) {

   //do something

} </lsl>

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index = -1; for (;index < lengthOfList;++index) {

   //do something

} </lsl>

and improve readability by using a while-loop:

if indexes can be negative if indexes can NOT be negative
<lsl>

list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

// it's bitwise-NOT (~) not minus (-) integer index = ~llGetListLength(listOfTenItems); while (++index) {

   //do something

} </lsl>

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index = -1; while (++index < lengthOfList) {

   //do something

} </lsl>

finally clean code

and try to get rid of the bitwise-NOT because some people find it hard to read. The positive side effect of the next step is that you need to increment one step less in total and the loop runs the first step directly without checking first for a conditional statement:

if indexes can be negative if indexes can NOT be negative
<lsl>

list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];

// negative length now! integer index = -llGetListLength(listOfTenItems);

do {

   //do something

} while (++index); </lsl>

<lsl> list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; integer lengthOfList = llGetListLength(listOfTenItems);

integer index; do {

   //do something

} while (++index < lengthOfList); </lsl>