|
Arrays
Multidimensional arrays no longer exist as entities within
LScript. All arrays are now strictly linear. However,
arrays can be assigned to specific elements of other arrays,
allowing multi-dimensional arrays to be constructed by the
script as it executes. Such user-defined multidimensional
arrays are as dynamic as their linear counterparts, and
elements can be added to any point at will.
// generate a multidimensional array "on-the-fly"
a[3,5,2] = 0;
For efficiency, multi-dimensional arrays created in this
fashion are not "robust." For example, the above array
creation generates a three-element array, whose third
element is an array of five elements, whose fifth element
is an array of two elements, whose second element has a
value of zero (0). Put graphically:
a[1] -> nil
a[2] -> nil
a[3] -> [1] -> nil
[2] -> nil
[3] -> nil
[4] -> nil
[5] -> [1] -> nil
[2] -> 0
To create "robust" multidimensional arrays (where all
elements are fully populated), it must be formally
declared as all multidimensional arrays were required
to be in LScript v1.x:
var a[3][5][2];
Mult-dimensional arrays can be constructed by piecing
together linear arrays:
a[5] = "Bob";
b[3] = a;
b[3,5,2] = 'a'; // the string "Bob" now becomes "Bab"
Because arrays can now be members of other arrays,
arrays-as-elements can now be passed to user-defined
functions:
...
// sub-array passing to functions
a[5] = "Bob";
b[2] = 99.9;
b[3] = a;
c[8] = b;
info(c);
info(c[8]);
info(c[8,3]);
info(c[8,3,5]);
test(c);
test(c[8]);
test(c[8,3]);
test(c[8,3,5]);
}
test: a
{
info(a);
}
? (Inline Index)
An "inline index" is now available for use that allows you to select an item from a list of values based upon an expression that must evaluate to an integer index value. The syntax of the inline index is quite similar to that of the inline selection mechanism. Whereas inline selection utilizes the "?:" token pair to perform boolean (true/false) selection:
pinfo[count][WOBBLEMAG] = wobble ? 30 : 10;
The inline index uses the "?" operator to perform its indexing selection:
pinfo[count][WOBBLEMAG] = wobble ? @30,15,10@;
If the index value is less than 1 or greater than the highest offset of the list of values provided, then a 'nil' value will be returned.
Any data type capable of being indexed can be used on the right side of the "?" operator, while any expression that evaluates to an integer index value can be used on the left:
str = "This is my string";
x = selectChar() ? str; // if selectChar() returns 6, x becomes "i"
which = 1 ? 15; // 'which' becomes 15
which = 3 ? 15; // 'which' becomes 'nil'
mySel = offset[5] ? @1,2,3,4,5,6,7@;
mySel = offset[5] ? offset; // use element #5 value to select
// another element!
Associative Arrays
Associative arrays are indexed using string values instead of integer indices. Associative arrays are powerful mechanisms that can be used to simulate user-defined structures, such as those found in C or Pascal.
In associative arrays, the index value used (the character string) is the symbol to which attributes are associated. The attributes are contained in the arrays that are indexed using the symbol. Yes, it all sounds very confusing. It should be clearer, however, in practice.
fruit_color["apple"] = "red";
fruit_color["banana"] = "yellow";
...
fruit_shape["apple"] = "round";
fruit_shape["banana"] = "curved";
...
fruit_weight["apple"] = 128; // grams
fruit_weight["banana"] = 117.3; // grams
...
As you can see from this short example, the name of the instance of the structure is the index symbol ("apple", "banana", etc.). The individual members of each structure is actually the name of each array ("fruit_color", "fruit_shape", etc.).
Associative arrays can be created/initialized with the new associative initializer operator '$'. These new operators are used in a similar fashion to the initializer block operators ('@').
normal_array = @1,2,3,4@;
assoc_array = $ .. $;
In order to initialize an associative array, however, you must use pairs of elements. The first part of each pair must be a character string that is the associative symbol for the data found in the second part.
fruit_color = $ "banana", "yellow", "apple", "red" $;
Associative Assignment
Arrays can now be the source of an associative assignment.
...
colors = @"Red","Green","Blue"@;
(red,green) = colors; // assigns "Red" to red, and so on
...
This allows the contents of the 'this' container to be accessed
before the contents are overwritten.
...
if(command == s~([a-zA-Z]+) (.+)~)
{
(item1,item2) = this;
...
keys
A keys() method can be applied to an associative array to extract all keys contained
within the array. This allows a script to access all data elements contained in the
associative array in a linear fashion.
|
|