select: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<h3 style="display:none">Bottom Section</h3> " to "")
m (minor style fix)
 
(73 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Command|Comments=
{{RV|type=command


| ofp |Game name=
|game1= ofp
|version1= 1.00


|1.00|Game version=
|game2= ofpe
|version2= 1.00


|gr1= Arrays |GROUP1=
|game3= arma1
|version3= 1.00


|gr2= Strings |GROUP2=
|game4= arma2
|version4= 1.00


|gr3= Config |GROUP3=
|game5= arma2oa
|version5= 1.50


| Selects an element from an array, config entry from [[Config]] or substring from a string or a range from an array.
|game6= tkoh
{{Informative | Substring version of `select` operates with ANSI charset, if Unicode support is desired, see [[forceUnicode]] }}|DESCRIPTION=
|version6= 1.00


| array  '''select''' index |SYNTAX=
|game7= arma3
|version7= 0.50


| p1= array: [[Array]] |PARAMETER1=
|gr1= Arrays


| 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=
|gr2= Strings


| [[Anything]] - a <u>reference</u> to array element given by its index |RETURNVALUE=
|gr3= Config


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


| s2= array '''select''' boolean |SYNTAX2=
|s1= array [[select]] index


| p21= array: [[Array]] |PARAMETER21=
|p1= array: [[Array]]


| p22= boolean: [[Boolean]] - [[true]] => 1, [[false]] => 0|PARAMETER22=
|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.<br>
{{GVI|arma3|2.12|size= 0.75}} negative index can be used to select from the end of the array, i.e. -1 means last array element.


| r2= [[Anything]] - a <u>reference</u> to array element given by its index (false - 0, true - 1)|RETURNVALUE2=
|r1= [[Anything]] - a <u>reference</u> to the array element given by its index
{{Feature|important|When the index equals the size of the array, there is no error for out of range selection and [[nil]] is returned - see {{Link|#Example 8}}.}}


|s2= array [[select]] boolean


| s3= config  '''select''' index |SYNTAX3=
|p21= array: [[Array]]


| p41= config: [[Config]] |PARAMETER41=
|p22= boolean: [[Boolean]] - [[false]] selects the '''first''' element of the [[Array]], [[true]] the '''second''' one


| 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.  |PARAMETER42=
|r2= [[Anything]] - a <u>reference</u> to the array element


| r3= [[Config]] |RETURNVALUE3=
|s3= config [[select]] index


|s3since= arma1 1.00


| s4= string '''select''' [start, length] {{Since|arma3|1.27.126674|y}} |SYNTAX4=
|p41= config: [[Config]]


| p61= string: [[String]] |PARAMETER61=
|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


| p62= [start, length]: [[Array]] |PARAMETER62=
|r3= [[Config]]


| 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. |PARAMETER63=
|s4= string [[select]] [start, length]
| 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. |PARAMETER64=


| r4= [[String]] |RETURNVALUE4=
|s4since= arma3 1.28


| s5= array '''select''' [start, count] {{Since|arma3|1.31.127272|y}} |SYNTAX5=
|p61= string: [[String]]


| p81= array: [[Array]] |PARAMETER81=
|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


| p82= [start, count]: [[Array]] |PARAMETER82=
|p63= length: [[Number]] - (Optional, default ''string''<nowiki/>'s length) number of characters to select


| p83= start: [[Number]] - Array index to start selection from.|PARAMETER83=
|r4= [[String]]
| 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. |PARAMETER84=
{{Feature|informative|Substring version of [[select]] operates with the ANSI charset; if Unicode support is desired, see [[forceUnicode]].}}


| r5= [[Array]] - a <u>new array</u> from selection |RETURNVALUE5=
|s5= array [[select]] [start, count]


| s6= array '''select''' expression {{Since|arma3|1.55.133771|y}} |SYNTAX6=
|s5since= arma3 1.32


| p101= array: [[Array]] |PARAMETER101=
|p81= array: [[Array]]


| 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 |PARAMETER102=
|p82= start: [[Number]] - array index to start selection from


| r6= [[Array]] - a <u>new array</u> of all elements from the original array that satisfied expression condition |RETURNVALUE6=
|p83= count: [[Number]] - (Optional after {{GVI|arma3|2.14|size= 0.75}}) 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.
 
{{Feature|informative|Since v2.14 'count' is optional, if omitted the selection is made from ''start'' until the end of the array.}}
 
|x1= <code>["a", "b", "c", "d"] [[select]] 2; {{cc|result is "c"}}
[[position]] [[player]] [[select]] 2; {{cc|result is Z coordinate of player position}}</code>|EXAMPLE1=


|x2= <code>["", [[currentWeapon]] [[player]]] [[select]] [[alive]] [[player]]; {{cc|if player is dead, "" is selected}}</code> |EXAMPLE2=
|r5= [[Array]] - a <u>new array</u> from selection


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


|x4= <code>[[hint]] [[str]] ("japa is the man!" [[select]] [8]); {{cc|the man!}}
|s6since= arma3 1.56
[[hint]] [[str]] ("japa is the man!" [[select]] [0,7]); {{cc|japa is}}</code> |EXAMPLE4=


|x5= <code>[[hint]] [[str]] ([1,2,3,4,5,6] [[select]] [1,4]); {{cc|[2,3,4,5]}}</code> |EXAMPLE5=
|p101= array: [[Array]]


|x6= <code>_even = [1,2,3,4,5,6,7,8,9,0] [[select]] { _x % 2 == 0 }; {{cc|returns [2, 4, 6, 8, 0]}}</code> |EXAMPLE6=
|p102= expression: [[Code]] - expression that is expected to return [[Boolean]] or [[Nothing]].
If [[true]] is returned, the original array value of currently tested element [[Magic Variables#x|_x]] will be added to the output array


| [[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=
|r6= [[Array]] - a <u>new array</u> of all elements from the original array that satisfied expression condition
}}
 
|x1= <sqf>
["a", "b", "c", "d"] select 2; // result is "c"
position player select 2; // result is Z coordinate of player position
</sqf>
 
|x2= <sqf>["", currentWeapon player] select alive player; // if player is dead, "" is selected</sqf>
 
|x3= <sqf>(configFile >> "cfgVehicles" >> typeOf vehicle player >> "Turrets") select 0 >> "gunnerAction";</sqf>
 
|x4= <sqf>
hint str ("japa is the man!" select [8]); // the man!
hint str ("japa is the man!" select [0, 7]); // japa is
</sqf>
 
|x5= <sqf>hint str ([1,2,3,4,5,6] select [1, 4]); // [2,3,4,5]</sqf>
 
|x6= <sqf>_even = [1,2,3,4,5,6,7,8,9,0] select { _x % 2 == 0 }; // returns [2, 4, 6, 8, 0]</sqf>
 
|x7= JavaScript endsWith() alternative:
<sqf>
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
</sqf>
 
<!-- referenced in r1 -->
|x8= [[select]] index traps:
<sqf>
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];


<h3 style="display:none">Notes</h3>
// get the last element properly
<dl class="command_description">
_array select (count _array); // wrong - nil is returned
<!-- Note Section BEGIN -->
_array select (count _array - 1); // correct - "d" is returned
<dd class="notedate">Posted on 3 March 2009</dd>
</sqf>
<dt class="note">[[User:General Barron|General Barron]]</dt>
<dd class="note">
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.
</dd>


<dd class="notedate">Posted on 27 Sep, 2013</dd>
|x9= {{Feature|arma3|Since {{GVI|arma3|2.12}} {{hl|-1}} can be used to select the last element of an array.}}
<dt class="note">[[User:Killzone_Kid|Killzone_Kid]]</dt>
<sqf>private _array = [1, 2, 3, 4, 5];
<dd class="note">Rounding of fractions with [[select]] is not the same as when you use [[round]] command:
hint str (_array select -1); // 5</sqf>
<code>_roundThis = 0.5;
[[hint]] [[str]] ([0,1] [[select]] _roundThis); {{cc|0}}
[[hint]] [[str]] ([0,1] [[select]] [[round]] _roundThis); {{cc|1}}</code>
</dd>


<dd class="notedate">Posted on 14 juil, 2016
|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]]
<dt class="note">[[User:Pierre MGI|Pierre MGI]]<dd class="note">You can substract array from array using select:
}}
<code>_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]];
</code>
</dd>
</dl>
<!-- Note Section END -->


[[Category:Scripting Commands OFP 1.46|{{uc:{{PAGENAME}}}}]]
{{Note
[[Category:Scripting Commands OFP 1.96|{{uc:{{PAGENAME}}}}]]
|user= General Barron
[[Category:Scripting Commands OFP 1.99|{{uc:{{PAGENAME}}}}]]
|timestamp= 20090303020200
{{GameCategory|arma1|Scripting Commands}}
|text= 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.
{{GameCategory|arma2|Scripting Commands}}
See the notes under [[count]] for more info.
{{GameCategory|arma3|Scripting Commands}}
}}
{{GameCategory|tkoh|Scripting Commands}}
 
{{Note
|user= Killzone_Kid
|timestamp= 20130928005300
|text= Rounding of fractions with [[select]] is not the same as when you use [[round]] command:
<sqf>
_roundThis = 0.5;
hint str ([0,1] select _roundThis); // 0
hint str ([0,1] select round _roundThis); // 1
</sqf>
}}


<!-- CONTINUE Notes -->
{{Note
<dl class="command_description">
|user= Pierre MGI
<dd class="notedate">Posted on June 22, 2015 - 23:23 (UTC)</dd>
|timestamp= 20160714041800
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
|text= You can substract array from array using select:
<dd class="note">
<sqf>
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.
_array = [[1], [2], [3]];
<code>[] [[select]] 0; //ok, result is nil
_sub = [2];
[1,2,3] [[select]] 3; //ok, result is nil
_array - _sub; // [[1], [2], [3]]
[1,2,3] [[select]] 4; //division by zero</code>
_array select { !(_x isEqualTo _sub) }; // [[1], [3]]
</dd>
[[1],[2],[2],[2],[2],[3]] select { !(_x isEqualTo _sub) }; // [[1], [3]]
</sqf>
}}


<dd class="notedate">Posted on November 12, 2016 - 22:36 (UTC)</dd>
{{Note
<dt class="note">[[User:Commy2|Commy2]]</dt>
|user= Commy2
<dd class="note">
|timestamp= 20161112223600
It is not safe to escape the code block of alternative syntax #5 with exitWith, breakOut etc.<br>
|text= It is not safe to escape the code block of alternative syntax #5 with [[exitWith]], [[breakOut]] etc:
<code>x3 = [1,2,3,4,5] select {  
<sqf>
    if (_x == 3) exitWith {  
x3 = [1,2,3,4,5] select {
        false;  
if (_x == 3) exitWith
    };  
{
    true  
false;
};
true
};
};
// could be expected to be: x3 = [1,2,4,5]
// could be expected to be: x3 = [1,2,4,5]
// actual result: x3 = false
// actual result: x3 = false
</code>
</sqf>
</dd>
}}


<dd class="notedate">Posted on February 14, 2017 - 16:26 (UTC)</dd>
{{Note
<dt class="note">[[User:Igneous01|Igneous01]]</dt>
|user= Igneous01
<dd class="note">
|timestamp= 20170214162600
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.
|text= {{Link|#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
<sqf>
myEastGroups = allgroups select {side _x == EAST;}; // returns all groups that are side EAST
myAliveUnits = allUnits select { alive _x }; // alive returns a boolean, the last statement run was alive _x,
my4ManGroups = allgroups select { count (units _x) == 4;}; // returns all groups that have 4 men in them
// therefore this piece of code will return a true/false to the select command
UnitsThatDetectedMe = allunits select {_x knowsAbout player > 0.1;}; // returns a list of units that have detected the player
myEastGroups = allGroups select { side _x == east }; // returns all east groups
</code>
my4ManGroups = allGroups select { count units _x == 4 }; // returns all groups that have 4 men in them
</dd>
unitsThatDetectedMe = allUnits select { _x knowsAbout player > 0.1 }; // returns a list of units that have detected the player
 
</sqf>
<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>
 
<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>
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 17:01, 4 October 2024

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:
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.
Arma 3 logo black.png2.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
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

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
Substring version of select operates with the ANSI charset; if Unicode support is desired, see forceUnicode.

Syntax 5

Syntax:
array select [start, count]
Parameters:
array: Array
start: Number - array index to start selection from
count: Number - (Optional after Arma 3 logo black.png2.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.
Since v2.14 'count' is optional, if omitted the selection is made from start until the end 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
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:
Arma 3
Since Arma 3 logo black.png2.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

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
General Barron - c
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.
Killzone_Kid - c
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
Pierre MGI - c
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]]
Commy2 - c
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
Igneous01 - c
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