Array: Difference between revisions
Lou Montana (talk | contribs) m (Some wiki formatting) |
Lou Montana (talk | contribs) m (Fix) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
== Working | == Working With Arrays == | ||
=== Array | === Array Properties === | ||
An array variable is a '''reference''' to the array (see {{Link|https://en.wikipedia.org/wiki/Reference_(computer_science)|Wikipedia reference page}}); | An array variable is a '''reference''' to the array (see {{Link|https://en.wikipedia.org/wiki/Reference_(computer_science)|Wikipedia reference page}}); | ||
Line 28: | Line 28: | ||
</sqf> | </sqf> | ||
=== | === Array Creation === | ||
<sqf> | <sqf> | ||
Line 59: | Line 59: | ||
</sqf> | </sqf> | ||
=== Setting an | === Setting an Element === | ||
<sqf> | <sqf> | ||
Line 118: | Line 118: | ||
Sub-arrays are also deep-copied; {{hl|_myNewArray}} will not point at the same sub-array instances. | Sub-arrays are also deep-copied; {{hl|_myNewArray}} will not point at the same sub-array instances. | ||
=== Adding ( | === Adding (Appending) Elements === | ||
In {{arma3}} use [[append]] and [[pushBack]] commands: | In {{arma3}} use [[append]] and [[pushBack]] commands: | ||
Line 135: | Line 135: | ||
</sqf> | </sqf> | ||
=== Removing ( | === Removing (Deleting) Elements === | ||
In {{arma3}} the [[deleteAt]] and [[deleteRange]] commands are available: | In {{arma3}} the [[deleteAt]] and [[deleteRange]] commands are available: | ||
Line 177: | Line 177: | ||
</sqf> | </sqf> | ||
=== Going | === Going Through the Array === | ||
The simplest way to iterate through an array is the [[forEach]] command: | The simplest way to iterate through an array is the [[forEach]] command: | ||
Line 194: | Line 194: | ||
== Advanced | == Advanced Usage == | ||
=== apply === | === apply === | ||
Line 262: | Line 262: | ||
</sqf> | </sqf> | ||
=== Sorting | === Selective Removal === | ||
Selectively filter out single elements from {{hl|_arrayA}} based on {{hl|_arrayB}}.<br> | |||
This is useful in the case where ''some'' duplicates must be removed. | |||
<sqf> | |||
private _arrayA = [1, 2, 3, 2, 4, 5, 4]; | |||
private _arrayB = [2, 2, 4]; | |||
{ | |||
private _index = _arrayA find _x; | |||
if (_index != -1) then | |||
{ | |||
_arrayA deleteAt _index; | |||
}; | |||
} forEach _arrayB; | |||
_arrayA; // is now [1, 3, 5, 4] | |||
</sqf> | |||
=== Sorting === | |||
==== sort ==== | ==== sort ==== |
Latest revision as of 19:49, 26 May 2024
An Array is a list of items of varying variable types (including other arrays). Different types can coexist within the same array.
See also: Arrays
Working With Arrays
Array Properties
An array variable is a reference to the array (see Wikipedia reference page); this means that if the array is edited, all the scripts/functions using a reference to this array will see the edition.
An array set through setVariable does not need to be assigned again if you modify it by reference:
Array Creation
An array can hold another array within it, that can hold another array itself, etc:
Getting an element
An array uses a zero-based index for its elements:
Setting an Element
Counting elements
Changing array size
The resize command is made to reduce or expand an array:
Array Copy
In order to avoid this behaviour, copy the array with + (plus):
Sub-arrays are also deep-copied; _myNewArray will not point at the same sub-array instances.
Adding (Appending) Elements
In Arma 3 use append and pushBack commands:
You could also use the plus (+) operator to add arrays. The difference is that addition returns a copy of array and thus a little slower than append and pushBack, which modify the target array.
Removing (Deleting) Elements
In Arma 3 the deleteAt and deleteRange commands are available:
You can also use the minus (-) operator to subtract arrays. The subtraction returns array copy, just like addition, and is not as fast as deleteAt and deleteRange which modify target arrays.
In Arma 3 it became possible to also subtract nested arrays:
The subtraction will remove all elements of the second array from the first one:
The solution to this issue is the combined use of set and an item that you know is not present in the array:
Using this technique, it is possible to mimic deleteRange behaviour this way:
Going Through the Array
The simplest way to iterate through an array is the forEach command:
A combination of for, count and select can also be used:
Advanced Usage
apply
Similar to the "map" function in Javascript, apply allows to apply code to every elements in an array and return a copy:
select
A simple way to filter an array (and obtain a new one) is using select's alternative syntax:
findIf
The findIf command was introduced in Arma 3 and allows you to go through the whole list and stop as soon as the condition is met, returning the condition-meeting element's array index:
You could use count to achieve the same result, however count won't stop until it iterated through the whole array, so it might take longer.
arrayIntersect
The arrayIntersect command returns a new array filled with the items found in both provided lists:
You can remove duplicates (get unique items) with this command:
Be wary that nil elements get removed by this method:
Selective Removal
Selectively filter out single elements from _arrayA based on _arrayB.
This is useful in the case where some duplicates must be removed.
Sorting
sort
The sort command allows for sorting an array of String, Number or sub-Arrays of string/number. It modifies the original array and does not return anything:
reverse
The reverse command simply reverses the array order:
BIS_fnc_sortBy
The function BIS_fnc_sortBy has been created for more complex sorting. Its algorithm input must return a number:
Common Errors
Index Rounding
In Real Virtuality scripts, indices are rounded to the nearest whole number. A boundary case (X.5, where X is any whole number) rounds to the nearest even whole number.
- Boundary cases
- -0.5 <= index <= 0.5 rounds to 0
- 0.5 < index < 1.5 rounds to 1
- 1.5 <= index <= 2.5 rounds to 2
- 2.5 < index < 3.5 rounds to 3
- In short
- -0.5 rounds up to 0
- 0.5 rounds down to 0
- 1.5 rounds up to 2
- 2.5 rounds down to 2
- 3.5 rounds up to 4
etc.
Index out of Range
The following code lists Arma 3 behaviour on wrong indices:
param
Use the param command in order to avoid out of range error:
set
If the index given to the set command is out of bounds:
- if the index rounded to a negative number, then an Error Zero Divisor message will be displayed in game.
- if the index rounded to a positive number, then the array will resize to incorporate the index as its last value. Each element between the last valid element, and the new set element, will be the null type
Zero Divisor
See above - a used array index may be negative.
Unexpected ","
This error originates from a bad syntax:
Reserved variable in expression
This issue arises when trying to modify a read-only array (such as select arrays that are returned from an addon, or eventually a trigger's thisList).
To combat this, copy said array with the plus (+) command before modifying: