Arma 3 Scripting

select

  • Arma 3 v0.50
  • First in Operation Flashpoint v1.00

Command · also in Strings, Config · Markdown · JSON · original wiki page

Description

Selects an element from an array, config entry from Config or substring from a string or a range from an array.

Syntax

Syntax 1

array select index

Parameters:

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.

Syntax 2

array select boolean

Parameters:

Returns: Anything - a reference to the array element

Syntax 3

since arma1 1.00

config select index

Parameters:

Returns: Config

Syntax 4

since arma3 1.28

string select [start, length]

Parameters:

Returns: String

Note

Substring version of select operates with the ANSI charset; if Unicode support is desired, see forceUnicode.

Syntax 5

since arma3 1.32

array select [start, count]

Parameters:

Returns: Array - a new array from selection

Syntax 6

since arma3 1.56

array select expression

Parameters:

Returns: Array - a new array of all elements from the original array that satisfied expression condition

Examples

Example 1

["a", "b", "c", "d"] select 2;    // result is "c"
position player select 2;        // result is Z coordinate of player position

Example 2

["", currentWeapon player] select alive player; // if player is dead, "" is selected

Example 3

(configFile >> "cfgVehicles" >> typeOf vehicle player >> "Turrets") select 0 >> "gunnerAction";

Example 4

hint str ("japa is the man!" select [8]);        // the man!
hint str ("japa is the man!" select [0, 7]);    // japa is

Example 5

hint str ([1,2,3,4,5,6] select [1, 4]); // [2,3,4,5]

Example 6

_even = [1,2,3,4,5,6,7,8,9,0] select { _x % 2 == 0 }; // returns [2, 4, 6, 8, 0]

Example 7

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

Example 8

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

Example 9

Note

Arma 3: Since Arma 3 v2.12 -1 can be used to select the last element of an array.

private _array = [1, 2, 3, 4, 5];
hint str (_array select -1); // 5

See also

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

Community notes

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.

Pierre MGI

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]]

Commy2

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

Igneous01

#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