select: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) |
Lou Montana (talk | contribs) (Add Syntax 3's introduction game) |
||
Line 29: | Line 29: | ||
|descr= Selects an element from an array, config entry from [[Config]] or substring from a string or a range from an array. | |descr= Selects an element from an array, config entry from [[Config]] or substring from a string or a range from an array. | ||
{{Feature | | {{Feature | informative | Substring version of `select` operates with ANSI charset, if Unicode support is desired, see [[forceUnicode]]. }} | ||
|s1= array | |s1= array [[select]] index | ||
|p1= array: [[Array]] | |p1= array: [[Array]] | ||
|p2= index: [[Number]] - | |p2= 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 | ||
|r1= [[Anything]] - a <u>reference</u> to array element given by its index | |r1= [[Anything]] - a <u>reference</u> to array element given by its index | ||
|s2= array | |s2= array [[select]] boolean | ||
|p21= array: [[Array]] | |p21= array: [[Array]] | ||
|p22= boolean: [[Boolean]] - [[ | |p22= boolean: [[Boolean]] - [[false]] selects the '''first''' element of the [[Array]], [[true]] the '''second''' one | ||
|r2= [[Anything]] - a <u>reference</u> to array element | |r2= [[Anything]] - a <u>reference</u> to the array element | ||
|s3= config | |s3= config [[select]] index | ||
|s3since= arma1 1.00 | |||
|p41= config: [[Config]] | |p41= config: [[Config]] | ||
|p42= index: [[Number]] - | |p42= 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 | ||
|r3= [[Config]] | |r3= [[Config]] | ||
Line 61: | Line 63: | ||
|p61= string: [[String]] | |p61= string: [[String]] | ||
|p62= | |p62= 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 | ||
|p63 | |p63= length: [[Number]] - (Optional, default ''string''<nowiki/>'s length) number of characters to select | ||
|r4= [[String]] | |r4= [[String]] | ||
Line 75: | Line 75: | ||
|p81= array: [[Array]] | |p81= array: [[Array]] | ||
|p82= | |p82= start: [[Number]] - array index to start selection from | ||
|p83 | |p83= count: [[Number]] - 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. | ||
|r5= [[Array]] - a <u>new array</u> from selection | |r5= [[Array]] - a <u>new array</u> from selection | ||
Line 109: | Line 108: | ||
{ | { | ||
[[params]] ["_string", "_endswith"]; | [[params]] ["_string", "_endswith"]; | ||
_string [[select]] <nowiki | _string [[select]] [<nowiki/>[[count]] _string - [[count]] _endswith] [[isEqualTo]] _endswith | ||
}; | }; | ||
["Arma 3", "3"] [[call]] _fnc_endsWith; | ["Arma 3", "3"] [[call]] _fnc_endsWith; {{cc|true}} | ||
["Arma 3", "4"] [[call]] _fnc_endsWith; {{cc|false}} | |||
</code> | |||
|seealso= [[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]] | |seealso= [[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]] | ||
Line 173: | Line 174: | ||
|user= Igneous01 | |user= Igneous01 | ||
|timestamp= 20170214162600 | |timestamp= 20170214162600 | ||
|text= Syntax | |text= {{HashLink|#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. | ||
<code>myAliveUnits = [[allUnits]] [[select]] { [[alive]] _x }; {{cc|[[alive]] returns a boolean, the last statement run was alive _x,}} | <code>myAliveUnits = [[allUnits]] [[select]] { [[alive]] _x }; {{cc|[[alive]] returns a boolean, the last statement run was alive _x,}} | ||
{{cc|therefore this piece of code will return a true/false to the select command}} | {{cc|therefore this piece of code will return a true/false to the select command}} |
Revision as of 22:25, 23 January 2022
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
- Return Value:
- Anything - a reference to 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 - 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:
["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
Additional Information
- 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
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:
_roundThis = 0.5; hint str ([0,1] select _roundThis); // 0 hint str ([0,1] select round _roundThis); // 1
- Posted on Jul 14, 2016 - 04:18 (UTC)
-
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]]
- Posted on Jun 22, 2015 - 23:23 (UTC)
-
Usually when select tries to pick up element out of range, Arma throws "division by zero" error. However there are exceptions. Basically as long as index of element you are selecting is less then or equal to array size, you will get no error.
[] select 0; // ok, result is nil [1,2,3] select 3; // ok, result is nil [1,2,3] select 4; // division by zero
- 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:
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
- 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