1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
Array
=====
An Array is a collection of values organized in sequencial fashion. To instantiate an Array, use the `[ value_1, value_2, ..., value_n ]` syntax rather than the `spawn()` function.
Example:
```
object "Application"
{
// The 'characters' array hold 3 strings
characters = [ "Surge", "Neon", "Charge" ];
state "main"
{
// who are the characters?
Console.print("The characters are:");
Console.print(characters[0]);
Console.print(characters[1]);
Console.print(characters[2]);
// how many characters?
Console.print("Number of characters: " + characters.length);
}
}
```
Output:
```
The characters are:
Surge
Neon
Charge
Number of characters: 3
```
> **Note:**
>
> Whenever you define a array, you spawn a new object. One is advised to **NOT** define arrays within states, because the code within states run continuously. Therefore, new objects will be created at every frame, not just once.
Properties
----------
#### length
`length`: number, read-only.
The number of elements in the Array.
Functions
---------
#### get
`get(index)`
Gets the specified element of the Array. Instead of calling `get()` directly, you may equivalently use the `[ ]` operator.
*Arguments*
* `index`: integer number between `0` and `this.length - 1`, inclusive.
*Returns*
The Array element at position `index` (0-based).
*Example*
```
characters = [ "Surge", "Neon", "Charge" ];
...
surge = characters[0]; // first element ("Surge")
neon = characters[1]; // second element ("Neon")
charge = characters[2]; // third element ("Charge")
```
#### set
`set(index, value)`
Sets to `value` the element of the Array at position `index`. Instead of calling `set()` directly, you may equivalently use the `[ ]` operator.
*Arguments*
* `index`: integer number between `0` and `this.length - 1`, inclusive.
* `value`: any type. The new value to be placed on the Array.
*Example*
```
characters = [ "Surge", "Neon", "Charge" ];
characters[0] = "Gimacian";
Console.print(characters[0]); // Gimacian
```
#### push
`push(value)`
Adds a new element to the end of the Array.
*Arguments*
* `value`: any type. The element to be added to the Array.
*Example*
```
characters = [ "Surge", "Neon", "Charge" ];
characters.push("Gimacian");
Console.print(characters); // [ "Surge", "Neon", "Charge", "Gimacian" ]
```
#### pop
`pop()`
Removes the last element of the Array and returns it.
*Returns*
The removed element.
#### shift
`shift()`
Removes the first element of the Array and returns it.
*Returns*
The removed element.
#### unshift
`unshift(value)`
Adds a new element to the beginning of the Array.
*Arguments*
* `value`: any type. The element to be added to the Array.
#### clear
`clear()`
Clears the array. It will no longer hold any elements and its length will be set to zero.
*Available since:* SurgeScript 0.5.3
#### indexOf
`indexOf(value)`
Search the Array for element `value` and return its position.
*Arguments*
* `value`: any type. The element to be searched for.
*Returns*
The position of the searched element - a number between `0` and `this.length - 1`, inclusive. If the desired element is not found, this function returns `-1`.
*Example*
```
characters = [ "Surge", "Neon", "Charge" ];
...
two = characters.indexOf("Neon"); // 2
gimacian = characters.indexOf("Gimacian"); // -1
if(gimacian < 0)
Console.print("Not found");
```
#### sort
`sort(cmpFun)`
Sorts the Array. If no comparison [functor](/tutorials/advanced_features#functors) is provided (`cmpFun` is `null`), the Array will be sorted in ascending order.
*Arguments*
* `cmpFun`: object | null. This [functor](/tutorials/advanced_features#functors) implements function `call(a, b)` that compares two array elements as described in the example below.
*Returns*
The sorted array. The returned array is the same array as you called `sort()` on; it's not a copy.
*Example*
```
// this example will print the elements
// of the Array in ascending order
object "Application"
{
arr = [ 3, 9, 4, 8, 5, 6, 7, 1, 2, 0 ];
state "main"
{
// sort and print the Array
arr.sort(null);
Console.print(arr);
Application.exit();
}
}
```
Output: `[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]`
```
// this example will print the elements
// of the Array in descending order
object "Application"
{
arr = [ 3, 9, 4, 8, 5, 6, 7, 1, 2, 0 ];
cmp = spawn("Sort.OrderByDesc");
state "main"
{
// sort and print the Array
arr.sort(cmp);
Console.print(arr);
Application.exit();
}
}
object "Sort.OrderByDesc"
{
// This function compares two
// elements of the Array: a and b.
//
// It should return a number:
// < 0 if a must come BEFORE b
// = 0 if a and b are equivalent
// > 0 if a must come AFTER b
fun call(a, b)
{
// sort in descending order
return b - a;
}
}
```
Output: `[ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]`
#### reverse
`reverse()`
Reverses the order of the elements in the Array.
#### shuffle
`shuffle()`
Shuffles the elements of the Array, placing its elements at random spots.
#### iterator
`iterator()`
Spawns an iterator.
*Returns*
An iterator to loop through the elements of the Array.
#### toString
`toString()`
Converts the Array to a string.
*Returns*
A string.
|