select: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (template:command argument fix)
(9 intermediate revisions by 9 users not shown)
Line 7: Line 7:
____________________________________________________________________________________________
____________________________________________________________________________________________


| Selects an element from an array, config entry from [[Config]] or substring from a string or a range from an array.|= Description
| Selects an element from an array, config entry from [[Config]] or substring from a string or a range from an array.|DESCRIPTION=
____________________________________________________________________________________________
____________________________________________________________________________________________


| array  '''select''' index |= Syntax
| array  '''select''' index |SYNTAX=


| p1= array : [[Array]] |= Parameter 1
| p1= array : [[Array]] |PARAMETER1=


| 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.  |= Parameter 2
| 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.  |PARAMETER2=


| [[Anything]] |= Return value
| [[Anything]] |RETURNVALUE=


____________________________________________________________________________________________
____________________________________________________________________________________________


| s2= array  '''select''' boolean |= Syntax
| s2= array  '''select''' boolean |SYNTAX=


| p21= array : [[Array]] |= Parameter 1
| p21= array : [[Array]] |PARAMETER1=


| p22= boolean: [[Boolean]] - [[true]] => 1, [[false]] => 0|= Parameter 2
| p22= boolean: [[Boolean]] - [[true]] => 1, [[false]] => 0|PARAMETER2=


| r2= [[Anything]] |= Return value
| r2= [[Anything]] |RETURNVALUE=


____________________________________________________________________________________________
____________________________________________________________________________________________


| s3= config  '''select''' index |= Syntax
| s3= config  '''select''' index |SYNTAX=


| p41= config : [[Config]] |= Parameter 1
| p41= config : [[Config]] |PARAMETER1=


| 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.  |= Parameter 2
| 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.  |PARAMETER2=


| r3= [[Config]] |= Return value
| r3= [[Config]] |RETURNVALUE=


____________________________________________________________________________________________
____________________________________________________________________________________________


| s4= string '''select''' [start, length]           ''(since ["Arma 3","Arma3",127,126674,"Development"])'' |= Syntax
| s4= string '''select''' [start, length]           ''(since ["Arma 3","Arma3",127,126674,"Development"])'' |SYNTAX=


| p61= string: [[String]] |= Parameter 1
| p61= string: [[String]] |PARAMETER1=


| p62= [start, length]: [[Array]] |= Parameter 2
| p62= [start, length]: [[Array]] |PARAMETER2=


| p63= 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. |= Parameter 3
| p63= 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. |PARAMETER3=
| p64= length (optional): [[Number]] - Number of the string characters to select. If "length" is omitted, selection will be made from "start" to the end of the string. |= Parameter 4
| p64= length (optional): [[Number]] - Number of the string characters to select. If "length" is omitted, selection will be made from "start" to the end of the string. |PARAMETER4=


| r4= [[String]] |= Return value
| r4= [[String]] |RETURNVALUE=
____________________________________________________________________________________________
____________________________________________________________________________________________


| s5= array '''select''' [start, count]           ''(since ["Arma 3","Arma3",131,127272,"Development"])''|= Syntax
| s5= array '''select''' [start, count]           ''(since ["Arma 3","Arma3",131,127272,"Development"])''|SYNTAX=


| p81= array: [[Array]] |= Parameter 1
| p81= array: [[Array]] |PARAMETER1=


| p82= [start, count]: [[Array]] |= Parameter 2
| p82= [start, count]: [[Array]] |PARAMETER2=


| p83= start: [[Number]] - Array index to start selection from.|= Parameter 3
| p83= start: [[Number]] - Array index to start selection from.|PARAMETER3=
| p84= 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. |= Parameter 4
| p84= 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. |PARAMETER4=


| r5= [[Array]] |= Return value
| r5= [[Array]] |RETURNVALUE=
____________________________________________________________________________________________
____________________________________________________________________________________________


| s6= array '''select''' expression           ''(since ["Arma 3","Arma3",155,133771,"Development"])''|= Syntax
| s6= array '''select''' expression           ''(since ["Arma 3","Arma3",155,133771,"Development"])''|SYNTAX=


| p101= array: [[Array]] |= Parameter 1
| p101= array: [[Array]] |PARAMETER1=


| p102= 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 |= Parameter 2
| p102= 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 |PARAMETER2=


| r6= [[Array]] - array of all elements from the original array that satisfied expression condition |= Return value
| r6= [[Array]] - array of all elements from the original array that satisfied expression condition |RETURNVALUE=
____________________________________________________________________________________________
____________________________________________________________________________________________
   
   
   
   
|x1= <code>[1,2,3,4] [[select]] 2; //result is 3
|x1= <code>[1,2,3,4] [[select]] 2; //result is 3
[[position]] [[player]] [[select]] 2; //result is Z coordinate of player position</code>|= Example 1
[[position]] [[player]] [[select]] 2; //result is Z coordinate of player position</code>|EXAMPLE1=


|x2= <code>["", [[currentWeapon]] [[player]]] [[select]] [[alive]] [[player]]; //if dead "" is selected</code>|= Example 2
|x2= <code>["", [[currentWeapon]] [[player]]] [[select]] [[alive]] [[player]]; //if dead "" is selected</code>|EXAMPLE2=


|x3= <code>([[configFile]] >> "cfgVehicles" >> [[typeOf]] [[vehicle]] [[player]] >> "Turrets") [[select]] 0 >> "gunnerAction";</code>|= Example 3
|x3= <code>([[configFile]] >> "cfgVehicles" >> [[typeOf]] [[vehicle]] [[player]] >> "Turrets") [[select]] 0 >> "gunnerAction";</code>|EXAMPLE3=




|x4= <code>[[hint]] [[str]] ("japa is the man!" [[select]] [8]); //the man!
|x4= <code>[[hint]] [[str]] ("japa is the man!" [[select]] [8]); //the man!
[[hint]] [[str]] ("japa is the man!" [[select]] [0,7]); //japa is</code>|= Example 4
[[hint]] [[str]] ("japa is the man!" [[select]] [0,7]); //japa is</code>|EXAMPLE4=


|x5= <code>[[hint]] [[str]] ([1,2,3,4,5,6] [[select]] [1,4]); //[2,3,4,5]</code>|= Example 5
|x5= <code>[[hint]] [[str]] ([1,2,3,4,5,6] [[select]] [1,4]); //[2,3,4,5]</code>|EXAMPLE5=


|x6= <code>_even = [1,2,3,4,5,6,7,8,9,0] [[select]] {_x%2 == 0}; // returns [2, 4, 6, 8, 0]</code>|= Example 6
|x6= <code>_even = [1,2,3,4,5,6,7,8,9,0] [[select]] {_x%2 == 0}; // returns [2, 4, 6, 8, 0]</code>|EXAMPLE6=
____________________________________________________________________________________________
____________________________________________________________________________________________


| [[selectRandom]], [[set]], [[resize]], [[reverse]], [[in]], [[find]], [[toArray]], [[toString]], [[forEach]], [[count]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[param]], [[params]], [[splitString]], [[joinString]], [[pushBack]], [[pushBackUnique]], [[apply]]|= 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]]|SEEALSO=  


}}
}}
Line 114: Line 114:
<code>_array = [[1],[2],[3]]; _sub = [2];
<code>_array = [[1],[2],[3]]; _sub = [2];
_array - _sub //  [[1],[2],[3];
_array - _sub //  [[1],[2],[3];
_array select {!(_x isEqualTo _sub)} // [[1],[3]];
_array [[select]] {!(_x [[isEqualTo]] _sub)} // [[1],[3]];
[[1],[2],[2],[2],[2],[3]] select {!(_x isEqualTo _sub)} // [[1],[3]];
[[1],[2],[2],[2],[2],[3]] [[select]] {!(_x [[isEqualTo]] _sub)} // [[1],[3]];
</code>
</code>
</dd>
</dd>
Line 141: Line 141:
[1,2,3] [[select]] 3; //ok, result is nil
[1,2,3] [[select]] 3; //ok, result is nil
[1,2,3] [[select]] 4; //division by zero</code>
[1,2,3] [[select]] 4; //division by zero</code>
</dd>
</dl>
<!-- DISCONTINUE Notes -->
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on November 12, 2016 - 22:36 (UTC)</dd>
<dt class="note">[[User:Commy2|Commy2]]</dt>
<dd class="note">
It is not safe to escape the code block of alternative syntax #5 with exitWith, breakOut etc.<br>
<code>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
</code>
</dd>
</dl>
<!-- DISCONTINUE Notes -->
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on February 14, 2017 - 16:26 (UTC)</dd>
<dt class="note">[[User:Igneous01|Igneous01]]</dt>
<dd class="note">
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;}; // 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 groups that are side EAST
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
</code>
</dd>
</dl>
<!-- DISCONTINUE Notes -->
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on May 28, 2017 - 13:51 (UTC)</dd>
<dt class="note">[[User:IT07|IT07]]</dt>
<dd class="note">
Very simple example of how to report about the status of the player:
<code>[ "Player is dead.", "Player is alive" ] select ( alive player )</code>
returns "Player is alive" because ( alive player ) returned true. If ( alive player ) returned false, the first element (0)
would have been returned.
That is a great way of reporting about something without having to write a complicated it then else statement.
</dd>
</dl>
<!-- DISCONTINUE Notes -->
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on April 14, 2018 - 13:06 (UTC)</dd>
<dt class="note">[[User:bloodwyn1756|bloodwyn1756]]</dt>
<dd class="note">
Since 1.82 "#" symbol can be used to select from an array. It's shorter to write and has higher priority than math functions.
</dd>
</dd>
</dl>
</dl>
<!-- DISCONTINUE Notes -->
<!-- DISCONTINUE Notes -->

Revision as of 15:46, 7 April 2019

Hover & click on the images for description

Description

Description:
Selects an element from an array, config entry from Config or substring from a string or a range from an array.
Groups:
Uncategorised

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

Syntax 2

Syntax:
array select boolean
Parameters:
array : Array
boolean: Boolean - true => 1, false => 0
Return Value:
Anything

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]           (since ["Arma 3","Arma3",127,126674,"Development"])
Parameters:
string: String
[start, length]: Array
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 (optional): Number - Number of the string characters to select. If "length" is omitted, selection will be made from "start" to the end of the string.
Return Value:
String

Syntax 5

Syntax:
array select [start, count]           (since ["Arma 3","Arma3",131,127272,"Development"])
Parameters:
array: Array
[start, count]: 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

Syntax 6

Syntax:
array select expression           (since ["Arma 3","Arma3",155,133771,"Development"])
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 - array of all elements from the original array that satisfied expression condition

Examples

Example 1:
[1,2,3,4] select 2; //result is 3 position player select 2; //result is Z coordinate of player position
Example 2:
["", currentWeapon player] select alive player; //if 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]

Additional Information

See also:
a hash bselectRandomselectRandomWeightedsetresizereverseinfindfindIftoArraytoStringforEachcountdeleteAtdeleteRangeappendsortparamparamssplitStringjoinStringpushBackpushBackUniqueapply

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

Notes

Posted on 3 March 2009
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.
Posted on 27 Sep, 2013
Killzone_Kid
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 round _roundThis; //1
Posted on 30 May, 2014 - 1549
ffur2007slx2_5
In ArmA3 ver 1.18, Boolean type supported. Which true defaulted as 1 and false as 0. [0,1] select (56 > 40) // 1 [0,1,2] select ((!isNil "v") && false) // 0
Posted on 14 juil, 2016
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]];

Bottom Section

Posted on June 22, 2015 - 23:23 (UTC)
Killzone Kid
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 November 12, 2016 - 22:36 (UTC)
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
Posted on February 14, 2017 - 16:26 (UTC)
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 groups that are side EAST 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
Posted on May 28, 2017 - 13:51 (UTC)
IT07
Very simple example of how to report about the status of the player: [ "Player is dead.", "Player is alive" ] select ( alive player ) returns "Player is alive" because ( alive player ) returned true. If ( alive player ) returned false, the first element (0) would have been returned. That is a great way of reporting about something without having to write a complicated it then else statement.
Posted on April 14, 2018 - 13:06 (UTC)
bloodwyn1756
Since 1.82 "#" symbol can be used to select from an array. It's shorter to write and has higher priority than math functions.