General Barron
When combined with the count command, this can be used to read all entries out of a config; even when you don't know exactly how many entries there will be. See the notes under count for more info.
Results appear below as you type; use the arrow keys to move through them.
Command · also in Strings, Config · Markdown · JSON · original wiki page
Selects an element from an array, config entry from Config or substring from a string or a range from an array.
array select index
Parameters:
index: Number - index 0 denotes the first element, 1 the second, etc. If index has decimal places it gets rounded down for fractions less than or equal .5, otherwise it gets rounded up.
Arma 3 v2.12 negative index can be used to select from the end of the array, i.e. -1 means last array element.
Returns: Anything - a reference to the array element given by its index
Important
When the index equals the size of the array, there is no error for out of range selection and nil is returned - see #Example 8.
array select boolean
Parameters:
array: Array of Anythingboolean: Boolean - false selects the first element of the Array, true the second oneReturns: Anything - a reference to the array element
since arma1 1.00
config select index
Parameters:
config: Configindex: Number - index 0 denotes the first element, 1 the second, etc. If index has decimal places it gets rounded down for fractions less than or equal .5, otherwise it gets rounded upReturns: Config
since arma3 1.28
string select [start, length]
Parameters:
string: Stringstart: Number - string position to start selection from. 0 denotes the first character of the string, 1 the second, etc. If passed number has decimal places it gets rounded down for fractions less than or equal .5, otherwise it gets rounded uplength: Number - (Optional, default string's length) number of characters to selectReturns: String
Note
Substring version of select operates with the ANSI charset; if Unicode support is desired, see forceUnicode.
since arma3 1.32
array select [start, count]
Parameters:
start: Number - array index to start selection from
count: Number - (Optional after Arma 3 v2.14) number of array elements to select. If the selected range exceeds source array boundaries, selection will be made up to the last element of the array.
Note
Since v2.14 'count' is optional, if omitted the selection is made from start until the end of the array.
Returns: Array - a new array from selection
since arma3 1.56
array select expression
Parameters:
array: Array of Anythingexpression: Code - expression that is expected to return Boolean or Nothing.
If true is returned, the original array value of currently tested element _x will be added to the output arrayReturns: Array - a new array of all elements from the original array that satisfied expression condition
["a", "b", "c", "d"] select 2; // result is "c"
position player select 2; // result is Z coordinate of player position
["", currentWeapon player] select alive player; // if player is dead, "" is selected
(configFile >> "cfgVehicles" >> typeOf vehicle player >> "Turrets") select 0 >> "gunnerAction";
hint str ("japa is the man!" select [8]); // the man!
hint str ("japa is the man!" select [0, 7]); // japa is
hint str ([1,2,3,4,5,6] select [1, 4]); // [2,3,4,5]
_even = [1,2,3,4,5,6,7,8,9,0] select { _x % 2 == 0 }; // returns [2, 4, 6, 8, 0]
JavaScript endsWith() alternative:
private _fnc_endsWith =
{
params ["_string", "_endswith"];
_string select [count _string - count _endswith] isEqualTo _endswith
};
["Arma 3", "3"] call _fnc_endsWith; // true
["Arma 3", "4"] call _fnc_endsWith; // false
select index traps:
private _array = ["a", "b", "c", "d"];
_array select 0; // "a"
_array select 3; // "d"
_array select 4; // nil - no error shown
_array select 5; // error
// can sometimes be useful
private _firstEnemyNearMe = units opfor select { player distance _x < 10 } select 0; // nil if no enemies nearby
if (isNil "_firstEnemyNearMe") exitWith { systemChat "no enemy found" };
systemChat format ["enemy found: %1", name _firstEnemyNearMe];
// get the last element properly
_array select (count _array); // wrong - nil is returned
_array select (count _array - 1); // correct - "d" is returned
Note
Arma 3: Since Arma 3 v2.12
-1can be used to select the last element of an array.
private _array = [1, 2, 3, 4, 5];
hint str (_array select -1); // 5
a hash b · selectRandom · selectRandomWeighted · set · resize · reverse · in · find · findIf · toArray · toString · forEach · count · deleteAt · deleteRange · append · sort · param · params · splitString · joinString · pushBack · pushBackUnique · apply · forceUnicode
When combined with the count command, this can be used to read all entries out of a config; even when you don't know exactly how many entries there will be. See the notes under count for more info.
Rounding of fractions with select is not the same as when you use round command:
_roundThis = 0.5;
hint str ([0,1] select _roundThis); // 0
hint str ([0,1] select round _roundThis); // 1
You can substract array from array using select:
_array = [[1], [2], [3]];
_sub = [2];
_array - _sub; // [[1], [2], [3]]
_array select { !(_x isEqualTo _sub) }; // [[1], [3]]
[[1],[2],[2],[2],[2],[3]] select { !(_x isEqualTo _sub) }; // [[1], [3]]
It is not safe to escape the code block of alternative syntax #5 with exitWith, breakOut etc:
x3 = [1,2,3,4,5] select {
if (_x == 3) exitWith
{
false;
};
true
};
// could be expected to be: x3 = [1,2,4,5]
// actual result: x3 = false
#Syntax 5 is the equivalent of passing in a predicate that returns a boolean. In SQF, a piece of code will always return what the last executed command returned.
myAliveUnits = allUnits select { alive _x }; // alive returns a boolean, the last statement run was alive _x,
// therefore this piece of code will return a true/false to the select command
myEastGroups = allGroups select { side _x == east }; // returns all east groups
my4ManGroups = allGroups select { count units _x == 4 }; // returns all groups that have 4 men in them
unitsThatDetectedMe = allUnits select { _x knowsAbout player > 0.1 }; // returns a list of units that have detected the player