count: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(a better workaround to kronzky note since there is another command is available)
(expanded, formatted, example)
Line 9: Line 9:
____________________________________________________________________________________________
____________________________________________________________________________________________


| Returns a count for either
| Can be used to count:
* the number of elements in array  
* the number of elements in array
* the number of elements in array for which given condition is true
* the number of elements in array with condition
* the number of sub-entries in a config object
* the number of sub-entries in a config object
* the number of characters in a string (since ["Arma 3","Arma3",127,126674,"Development"])
|= Description
|= Description
____________________________________________________________________________________________
____________________________________________________________________________________________


| condition '''count''' array |= Syntax
| '''count''' array |= Syntax


|p1= condition: (optional) [[Code]] that must return [[true]] to be counted.
| p1= array: [[Array]] |= Parameter
<br>
The variable ''[[Magic Variables|_x]]'' will contain the currently tested element. |= Parameter 1


|p2= array: [[Array]] |= Parameter 2
| [[Number]] |= Return value
____________________________________________________________________________________________
 
| s2= condition '''count''' array |= Syntax
 
| p21= condition: [[Code]] that must return [[true]] for the tested element to be counted. The variable ''[[Magic Variables|_x]]'' will contain the currently tested element. |= Parameter 1


| [[Number]] |= Return value
| p22=  array: [[Array]] |= Parameter 2
 
| r2= [[Number]] |= Return value


____________________________________________________________________________________________
____________________________________________________________________________________________
|s2= '''count''' configname |= Syntax
| s3= '''count''' configname |= Syntax


|p21= configname: [[Config]] |= Parameter 1
| p41= configname: [[Config]] |= Parameter


|r2= [[Number]] |= Return value
| r3= [[Number]] |= Return value
____________________________________________________________________________________________
____________________________________________________________________________________________
 
|x1= <code>_cnt = [[count]] [0, 0, 1, 2]; // returns 4</code> |= Example 1
| s4= '''count''' string |= Syntax


|x2= <code>_cnt = [[count]] [[units]] [[group]] [[player]]; // returns number of units in player group</code> |= Example 2
| p61= string: [[String]] |= Parameter


|x3= <code>_fnd = [1, 9, 8, 3, 4, 4, 4, 5, 6];
| r4= [[Number]] |= Return value
{
    [[Magic Variables|_x]] == 4;
} [[count]] _fnd; // returns 3</code> |= Example 3


|x4= <code>_alive = {
    [[alive]] [[Magic Variables|_x]];
} [[count]] [[allUnits]]; // returns the number of living units</code>
<br>
<code>_cnt = [[count]] ([[configFile]] [[gtgt|>>]] "CfgVehicles");</code> |= Example 4
__
____________________________________________________________________________________________
____________________________________________________________________________________________
____________________________________________________________________________________________
 
|x1= <code>_cnt = [[count]] [0,0,1,2]; // returns 4
_cnt = [[count]] [[units]] [[group]] [[player]]; // returns number of units in player group</code> |= Example 1
|x2= <code>_cnt = {[[Magic Variables|_x]] == 4} [[count]] [1,9,8,3,4,4,4,5,6]; // returns 3
_cnt = {[[alive]] [[Magic Variables|_x]]} [[count]] [[allUnits]]; // returns the number of alive units</code> |= Example 2
|x3= <code>_cnt = [[count]] ([[configFile]] [[gtgt|>>]] "CfgVehicles");</code> |= Example 3
|x4= <code>[[hint]] [[str]] [[count]] "japa is the man!"; //16</code> |= Example 4
________________________________________________________________________________________


| [[set]], [[resize]], [[pushBack]], [[reverse]], [[select]], [[in]], [[find]], [[toArray]], [[toString]], [[forEach]], [[in Array]] |= See also
| [[set]], [[resize]], [[pushBack]], [[reverse]], [[select]], [[in]], [[find]], [[toArray]], [[toString]], [[forEach]], [[in Array]] |= See also

Revision as of 20:53, 12 August 2014

Hover & click on the images for description

Description

Description:
Can be used to count:
  • the number of elements in array
  • the number of elements in array with condition
  • the number of sub-entries in a config object
  • the number of characters in a string (since ["Arma 3","Arma3",127,126674,"Development"])
Groups:
Uncategorised

Syntax 1

Syntax:
count array
Parameters:
array: Array
Return Value:
Number

Syntax 2

Syntax:
condition count array
Parameters:
condition: Code that must return true for the tested element to be counted. The variable _x will contain the currently tested element.
array: Array
Return Value:
Number

Syntax 3

Syntax:
count configname
Parameters:
configname: Config
Return Value:
Number

Syntax 4

Syntax:
count string
Parameters:
string: String
Return Value:
Number

Examples

Example 1:
_cnt = count [0,0,1,2]; // returns 4 _cnt = count units group player; // returns number of units in player group
Example 2:
_cnt = {_x == 4} count [1,9,8,3,4,4,4,5,6]; // returns 3 _cnt = {alive _x} count allUnits; // returns the number of alive units
Example 3:
_cnt = count (configFile >> "CfgVehicles");
Example 4:
hint str count "japa is the man!"; //16

Additional Information

See also:
setresizepushBackreverseselectinfindtoArraytoStringforEachin Array

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 April 28, 2007 - 13:49
Kronzky
This conditional count command only works if all the elements in the tested array are of the same type as the tested element.
For example, the following code will created an error, since the elements are of different types (object, number, string): _arr = [ player,100,"one"]; {_x == "one"} count _arr;
Alternatively, to avoid the error use isEqualTo instead of ==. --KK
This one, on the other hand, where all elements are strings, just like the tested element, will return the correct result of 1: _arr = ["one","two","three"]; {_x == "one"} count _arr;
Posted on August 3, 2006 - 14:27
hardrock
Notes from before the conversion: Use this to calculate how many "M16" mags a soldier has left. {_x == "M16"} count magazines soldier1; Take care when using count to determine how many units are left alive in a group: count units group player or count units groupname Will return the number of units the leader of the group thinks are alive. If some units have been killed out of sight of other members of the group then it may take sometime for this to be the actual numbers in the group. To determine exactly how many units are really alive in a group use: {alive _x} count units group player; or {alive _x} count units groupname;

Bottom Section