select
Jump to navigation
Jump to search
Description
- Description:
- Selects an element from an array, config entry from Config or substring from a string or a range from an array.
- Groups:
- ArraysStringsConfig
Syntax 1
- Syntax:
- array select index
- Parameters:
- array: Array
- 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.
2.12 negative index can be used to select from the end of the array, i.e. -1 means last array element. - Return Value:
- Anything - a reference to the array element given by its index
Syntax 2
- Syntax:
- array select boolean
- Parameters:
- array: Array
- boolean: Boolean - false selects the first element of the Array, true the second one
- Return Value:
- Anything - a reference to the array element
Syntax 3
- Syntax:
- config select index
- Parameters:
- config: Config
- 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
- Return Value:
- Config
Syntax 4
- Syntax:
- string select [start, length]
- Parameters:
- string: String
- start: 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 up
- length: Number - (Optional, default string's length) number of characters to select
- Return Value:
- String
Syntax 5
- Syntax:
- array select [start, count]
- Parameters:
- array: Array
- start: Number - array index to start selection from
- count: Number - (Optional after 2.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.
- Return Value:
- Array - a new array from selection
Syntax 6
- Syntax:
- array select expression
- Parameters:
- array: Array
- expression: 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 array
- Return Value:
- Array - a new array of all elements from the original array that satisfied expression condition
Examples
- Example 1:
- Example 2:
- Example 3:
- Example 4:
- Example 5:
- Example 6:
- Example 7:
- JavaScript endsWith() alternative:
- 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 = allUnits 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:
Additional Information
- See also:
- # selectRandom selectRandomWeighted set resize reverse in find findIf toArray toString forEach count deleteAt deleteRange append sort param params splitString joinString pushBack pushBackUnique apply forceUnicode
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
- Posted on Mar 03, 2009 - 02:02 (UTC)
- 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.
- Posted on Sep 28, 2013 - 00:53 (UTC)
- Rounding of fractions with select is not the same as when you use round command:
- Posted on Jul 14, 2016 - 04:18 (UTC)
- You can substract array from array using select:
- Posted on Nov 12, 2016 - 22:36 (UTC)
- It is not safe to escape the code block of alternative syntax #5 with exitWith, breakOut etc:
- Posted on Feb 14, 2017 - 16:26 (UTC)
-
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
Categories:
- Scripting Commands
- Introduced with Operation Flashpoint version 1.00
- Operation Flashpoint: New Scripting Commands
- Operation Flashpoint: Scripting Commands
- Operation Flashpoint: Elite: Scripting Commands
- ArmA: Armed Assault: Scripting Commands
- Arma 2: Scripting Commands
- Arma 2: Operation Arrowhead: Scripting Commands
- Take On Helicopters: Scripting Commands
- Arma 3: Scripting Commands
- Command Group: Arrays
- Command Group: Strings
- Command Group: Config