count: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\|gr([0-9]+) = " to "|gr$1= ")
(formatting)
Line 1: Line 1:
{{RV|type=command
{{RV|type=command


| ofp
|game1= ofp


|1.00
|version1= 1.00


|arg=
|game2= ofpe
 
|version2= 1.00
 
|game3= arma1
 
|version3= 1.00
 
|game4= arma2
 
|version4= 1.00
 
|game5= arma2oa
 
|version5= 1.51
 
|game6= tkoh
 
|version6= 1.00
 
|game7= arma3
 
|version7= 0.50


|gr1= Arrays
|gr1= Arrays
Line 15: Line 37:
|gr4= HashMap
|gr4= HashMap


| Can be used to count:
|descr= Can be used to count:
* the number of elements in array (doesn’t actually count them per se but rather returns already known array size)
* The number of elements in array (Returns the already known array size known internally)
* the number of elements in array with condition
* The number of elements in array with condition
* the number of sub-entries in a config entry
* The number of sub-entries in a config entry
* the number of characters in an ANSI string ({{Since|arma3|1.27.126674}}). If Unicode support is desired, see [[forceUnicode]].
* The number of characters in an ANSI string  
** If Unicode support is desired, see [[forceUnicode]] {{Since|arma3|1.28|y}}


| [[count]] array
|s1= [[count]] array


|p1= array: [[Array]], [[String]], [[Config]] or [[HashMap]]
|p1= array: [[Array]], [[String]], [[Config]] or [[HashMap]]


| [[Number]]
|r1= [[Number]]


|s2= condition [[count]] array
|s2= condition [[count]] array


|p21= condition: [[Code]] that must return [[true]] for the tested element to be counted. The variable <tt>[[Magic Variables#x|_x]]</tt> will contain the currently tested element.
|p21= condition: [[Code]] - Condition that must return [[true]] for the tested element to be counted. The variable <tt>[[Magic Variables#x|_x]]</tt> will contain the currently tested element
{{Feature|arma3|If array contains different data types, use [[isEqualTo]] instead of [[==]]!}}


|p22= array: [[Array]]
|p22= array: [[Array]]
Line 47: Line 71:
|x5= <code>[[if]] ([[count]] _myHashMap < 1) [[then]] { [[hint]] "empty hashmap!"; };</code>
|x5= <code>[[if]] ([[count]] _myHashMap < 1) [[then]] { [[hint]] "empty hashmap!"; };</code>


|seealso= [[set]], [[resize]], [[pushBack]], [[pushBackUnique]], [[apply]], [[select]], [[reverse]], [[select]], [[in]], [[find]], [[toArray]], [[toString]], [[forEach]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[countFriendly]], [[countEnemy]], [[countUnknown]], [[countSide]], [[countType]], [[splitString]], [[joinString]], [[findIf]], [[forceUnicode]]
|seealso= [[apply]], [[select]], [[select]], [[in]], [[find]], [[countFriendly]], [[countEnemy]], [[countUnknown]], [[countSide]], [[countType]], [[findIf]], [[forceUnicode]]
}}
}}


<dl class="command_description">
<dl class="command_description">
<dd class="notedate">Posted on April 28, 2007 - 13:49</dd>
<dt class="note">[[User:Kronzky|Kronzky]]</dt>
<dd class="note">
This conditional count command ''only'' works if all the elements in the tested array are of the same [[Data Types|type]] as the tested element.<br>
For example, the following code will created an error, since the elements are of different types (object, number, string):
<code>_arr = [<nowiki/>[[player]],100,"one"]; {_x == "one"} [[count]] _arr;</code>
::Alternatively, to avoid the error use [[isEqualTo]] instead of ==. --KK<br>
This one, on the other hand, where all elements are strings, just like the tested element, will return the correct result of 1:
<code>_arr = ["one","two","three"]; {_x == "one"} [[count]] _arr;</code>
<dt><dt>
<dd class="notedate">Posted on August 3, 2006 - 14:27</dd>
<dd class="notedate">Posted on August 3, 2006 - 14:27</dd>
<dt class="note">[[User:Hardrock|hardrock]]</dt>
<dt class="note">[[User:Hardrock|hardrock]]</dt>
Line 71: Line 85:
<code>{ [[alive]] _x } [[count]] [[units]] groupname;</code>
<code>{ [[alive]] _x } [[count]] [[units]] groupname;</code>
</dl>
</dl>
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
{{GameCategory|arma1|Scripting Commands}}
{{GameCategory|arma2|Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}
{{GameCategory|tkoh|Scripting Commands}}


<dl class="command_description">
<dl class="command_description">

Revision as of 08:51, 29 April 2021

Hover & click on the images for description

Description

Description:
Can be used to count:
  • The number of elements in array (Returns the already known array size known internally)
  • The number of elements in array with condition
  • The number of sub-entries in a config entry
  • The number of characters in an ANSI string
Groups:
ArraysStringsConfigHashMap

Syntax

Syntax:
count array
Parameters:
array: Array, String, Config or HashMap
Return Value:
Number

Alternative Syntax

Syntax:
condition count array
Parameters:
condition: Code - Condition that must return true for the tested element to be counted. The variable _x will contain the currently tested element
Arma 3
If array contains different data types, use isEqualTo instead of ==!
array: Array
Return Value:
Number

Examples

Example 1:
count [0, 0, 1, 2]; // returns 4 count units group player; // returns number of units in player group
Example 2:
private _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:
private _cnt = count (configFile >> "CfgVehicles");
Example 4:
hint str count "japa is the man!"; // 16
Example 5:
if (count _myHashMap < 1) then { hint "empty hashmap!"; };

Additional Information

See also:
applyselectselectinfindcountFriendlycountEnemycountUnknowncountSidecountTypefindIfforceUnicode

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 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;
Posted on December 15, 2014 - 00:01 (UTC)
Heeeere's Johnny!
count can be (ab)used for a very fast and simple check if at least one element in an array fulfills a certain condition: if ({ if (_x fulfills condition) exitWith {1}; false } count _array isEqualTo 1) then { // do whatever here }; This code will exit the count loop as soon as it finds an element fulfilling the condition, leaving the count with the value of 1, hence make the larger if-condition be true.
If no array element fulfills the condition, the count will be 0 and the if-condition will be false.
Posted on December 29, 2014 - 21:23 (UTC)
Killzone Kid
Quit loop at first fulfilled condition (same as above but faster): {if (_x == 4) exitWith { // do something when we reach 4 }} count [1,2,3,4,5,6];
Posted on January 2, 2015 - 22:32 (UTC)
Heeeere's Johnny!
Using exitWith inside a count loop will overwrite the default functionality and make count return whatever the exitWith returns: _result = { if (_x isEqualTo 3) exitWith { "Hello" } } count [1,2,3,4,5]; // _result = "Hello"
Posted on August 22, 2016 - 19:41 (UTC)
Ebay
With the alternative syntax each iteration should result in an interior return of bool or nothing. Example: createDialog "RscFunctionsViewer"; { lbAdd [292901, _x]; } count ["first", "second", "third"]; lbAdd returns a number, so this throws "Error Type Number, expected Bool". Tested in A2OA 1.63.131129