Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

types:list [2017-09-20 08:27 SLT]
sei WIP refurbishing
types:list [2021-02-01 09:00 SLT] (current)
sei Info on passing lists by reference
Line 1: Line 1:
 $nav $nav
-===== List type =====+===== Type: list =====
  
 A list is a sequentially numbered collection of elements of other types. It's the closest thing LSL has to what other languages call an array. Lists can have any number of elements, from zero to as many as the available memory can hold. You can think of a list as a row of a spreadsheet in which each cell has an element that can be one of the LSL types, but instead of having the columns labelled "​A",​ "​B",​ and so on, they are numbered starting from zero. We refer to that numbering as the index or position of the element. A list is a sequentially numbered collection of elements of other types. It's the closest thing LSL has to what other languages call an array. Lists can have any number of elements, from zero to as many as the available memory can hold. You can think of a list as a row of a spreadsheet in which each cell has an element that can be one of the LSL types, but instead of having the columns labelled "​A",​ "​B",​ and so on, they are numbered starting from zero. We refer to that numbering as the index or position of the element.
Line 47: Line 47:
  
 Internally, if ''​L''​ is a list and ''​e''​ is of any type other than a list, ''​L + e''​ is more efficient than either ''​L + [e]''​ or ''​L + (list)e''​. Internally, if ''​L''​ is a list and ''​e''​ is of any type other than a list, ''​L + e''​ is more efficient than either ''​L + [e]''​ or ''​L + (list)e''​.
 +
 +That applies to code memory usage. However, when the element already exists in a list, it's more efficient, from the point of view of data memory usage, to append it using $lfn[llList2List]. See [[#Data memory usage]] below.
 </​WRAP>​ </​WRAP>​
  
Line 61: Line 63:
 If you want to really compare whether two lists ''​L1''​ and ''​L2''​ are equal, you can use this trick: they are equal if ''​L1 == L2 && llListFindList(L1,​ L2) == 0''​. This works fine in $Mono, but it can give an erroneous result in $LSO if both lists are empty. For $LSO, you can use ''​L1 == L2 && (llListFindList(L1,​ L2) == 0 || L1 == [])''​ instead. If you want to really compare whether two lists ''​L1''​ and ''​L2''​ are equal, you can use this trick: they are equal if ''​L1 == L2 && llListFindList(L1,​ L2) == 0''​. This works fine in $Mono, but it can give an erroneous result in $LSO if both lists are empty. For $LSO, you can use ''​L1 == L2 && (llListFindList(L1,​ L2) == 0 || L1 == [])''​ instead.
  
-===== Examples ​=====+==== Examples ====
  
 The following is a basic example that holds a list of all the people who touched it. If touched by the owner, it lists the people who have touched it so far. For simplicity, it has no clean-up or memory overflow guard, therefore it can result in a stack-heap collision error if many people touch it. The following is a basic example that holds a list of all the people who touched it. If touched by the owner, it lists the people who have touched it so far. For simplicity, it has no clean-up or memory overflow guard, therefore it can result in a stack-heap collision error if many people touch it.
Line 90: Line 92:
 } }
 </​file>​ </​file>​
 +
 +==== Data memory usage ====
 +
 +This section is intended for advanced scripters.
 +
 +In $Mono, the size of list elements is evaluated as follows:
 +
 +^  Type  ^  Memory ​ ^
 +| $ty[integer] | 16 |
 +| $ty[float] | 16 |
 +| $ty[string] | 18 + size of the string in UTF-16 (*)  |
 +| $ty[key] | 30 + size of the string in UTF-16 (*)  |
 +| $ty[vector] | 24 |
 +| $ty[rotation] | 32 |
 +
 +(*) That's typically 2 bytes per character, but there are some special characters that take 4 bytes each, e.g. <​html><​code>&#​x1d538;</​code></​html>​
 +
 +List elements can be reused. A reused element takes only 4 bytes regardless of the type. All elements can be reused by not extracting the elements from the list, but adding them to the list in list form. For example:
 +
 +<code lsl2>
 +list A = (list)1;
 +A += llList2List(A,​ 0, 0);
 +</​code>​
 +
 +This list takes only 20 bytes (16 for the integer and 4 for a reference to the same integer).
 +
 +Strings in particular can also be reused if they already exist in a list, as long as they are not the result of some operation ($lfn[llList2String] is not considered an operation, but addition of strings and other functions such as $lfn[llToLower],​ $lfn[llGetSubString],​ etc. are). If they are the result of an operation, they are not reused, but they are reusable. For example:
 +
 +<code lsl2>
 +// This uses 20 bytes, per the table above:
 +list A = (list)"​X";​
 +// This only adds 4 bytes to the list, because constant
 +// strings are reused if they have been used
 +// to assign them to some list element that
 +// still contains the unmodified string, in this
 +// case the first element of the list:
 +A += "​X";​
 +// This adds 20 bytes to the list because the
 +// string that results from a calculation is not
 +// reused, even if it matches an existing
 +// string:
 +A = A + ("​X"​ + ""​); ​
 +// However, it is reusable, so this element
 +// will take 4 bytes only:
 +A = A + llList2String(A,​ -1);
 +string B = "​Y";​
 +// This adds 20 bytes to the list:
 +A += B;
 +// This adds 4 bytes to the list:
 +A += B;
 +</​code>​
 +
 +Basically, the rule isn't whether the strings are equal, but whether they are handled verbatim without applying operations to them. The same trick fails with other types, including keys; adding e.g. a key variable to a list multiple times, results in one new instance of the key being counted for every addition. Same happens for integers, for example. But extracting them as lists still works.
 +
 +Lists are passed by reference to functions, meaning that when passing a list to a function, a new copy is not created, but instead the same list is used.
 +
 +=== Limits ===
 +
 +List size is only limited by available memory. However, list initializers (''​[''​ ... ''​]''​ literals used in global variables) and list constructors (''​[''​ ... ''​]''​ literals used in events or functions) have some limitations in $Mono:
 +
 +  * List initializers are limited to 4096 elements.
 +  * List constructors are limited by an internal stack limit on expressions. The stack limit is 1024, but part of it is used internally and while doing operations, so you can't use a list constructor with more than 1022 elements and assign it to a variable.
 +    * This same stack limit affects any kind of expression, not just list constructors. It would be too hard to describe how the stack is used by expressions,​ but if you really have an expression that complex, you can work around the limit by breaking it up into several assignments.