Differences

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

Link to this comparison view

wiki:scripting_portal:arrays [2018/10/23 01:59] (current)
Royale Mobian created
Line 1: Line 1:
 +====== YEngine arrays ======
 +ASSOCIATIVE ARRAYS
  
 +Arrays are associative in that the index can be any immutable type, and any given array can have a mix of index types and a mix of value types. In particular, the index of an array can be a list to provide multiple-dimension array functionality.
 +
 +(Note: For fixed-dimension arrays, see [[wiki:​scripting_portal:​yengine.objects|YEngine objects]]).
 +
 +The index can be type float, key, list, integer, rotation, string, vector.
 +
 +Unlike other LSL script types, arrays are mutable. So, unlike lists, arrays can be passed to a function, the function can modify the array, and the caller will see the changes made by the function, all without any array copying.
 +
 +Enabled by: ''​yoption arrays;''​
 +
 +^ Keywords: ^ Description:​ ^
 +| array | declare a variable to be an array, and make it initially empty |
 +| foreach | iterate through all elements in an array |
 +| in | keyword part of foreach statement |
 +| is | test array element to be a particular type |
 +| object | declare a variable that can hold any type |
 +| undef | test object or array element to hold undefined element |
 +
 +**Declare an array:**
 +<code ossl> '​array'​ name ';'​ </​code>​
 +
 +**Example:​**
 +<code ossl> array ar; </​code>​
 +
 +**Insert or replace element:**
 +<code ossl>
 +   name '​['​ index '​]'​ '​='​ value ';'​
 +</​code>​
 +\\   
 +^ Keywords: ^ Description:​ ^
 +| name | was declared with '​array'​ statement |
 +| index | lsl value of type float, key, list, integer, rotation, string, vector |
 +| value | lsl value of any type |
 +  ​
 +**Examples:​**
 +<code ossl>
 +       ar[3] = "​abcdef";​
 +       ​ar["​five"​] = [ "​one",​ "​two",​ "​three"​ ];
 +       ​ar[5.0,"​six"​] = 7;
 +</​code> ​  
 +**Delete element:**
 +<code ossl>
 +   name '​['​ index '​]'​ '​='​ undef ';'​
 +</​code>​
 +\\
 +       it is not an error to delete an element that does not exist
 +
 +**Example:​**
 +<code ossl>
 + ​ar["​five"​] = undef;
 +</​code>​
 +**Retrieve element:**
 +<code ossl>
 +   name '​['​ index '​]'​
 +</​code>​
 +\\
 +if element not defined, return value is '​undef'​
 +\\
 +**Example:​**
 +<code ossl>
 +       ​llOwnerSay((string)ar[3]);​
 +</​code>​
 +\\
 +**Test type of object:**
 +<code ossl>
 +   <​object>​ is <​type> ​ =>  integer 0 or 1
 +   <​object>​ is undef   ​=> ​ integer 0 or 1
 +</​code> ​  
 +<code ossl>
 +   ​object o;
 +   if (o is integer) ...
 +   if (o is string) ... etc
 +</​code>​
 +\\
 +**Methods and Properties:​**
 +<code ossl>
 +   ​ar.clear() ​ // empty array, returns type void
 +   ​ar.count ​   // get number of elements in array, returns type integer
 +   ​ar.index(i) // get index of i'th (zero based) element, returns type object
 +   ​ar.value(i) // get value of i'th (zero based) element, returns type object
 +</​code>​
 +\\
 +**Iterate through all elements:**
 +<code ossl>
 +   array ar;
 +   ​object k;
 +   ​object v;
 +   ​foreach (k,v in ar) {
 +       k holds the index
 +       v holds the value
 +       ​llOwnerSay("​ar["​ + (string)k + "​]="​ + (string)v);
 +   }
 +
 +   // alternatively:​
 +
 +   ​object k;
 +   ​object v;
 +   ​integer n = ar.count;
 +   for (integer i = 0; i < n; i ++) {
 +       k = ar.index(i);​
 +       v = ar.value(i);​
 +       ​llOwnerSay((string)i + ": ar[" + (string)k + "​]="​ + (string)v);
 +   }
 +</​code>​
QR Code
QR Code wiki:scripting_portal:arrays (generated for current page)