count: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Some wiki formatting)
m (Text replacement - "<sqf>([^↵][^<]*↵[^<]*)<\/sqf>" to "<sqf> $1 </sqf>")
 
(10 intermediate revisions by 2 users not shown)
Line 34: Line 34:
* The number of elements in an array matching the condition
* The number of elements in an array matching the condition
* The number of sub-entries in a config entry
* The number of sub-entries in a config entry
* {{GVI|arma3|1.28|size=0.8}} The number of characters in an ANSI string {{Feature|informative|If Unicode support is desired, see [[forceUnicode]].}}
* {{GVI|arma3|1.28|size= 0.75}} The number of characters in an ANSI string {{Feature|informative|If Unicode support is desired, see [[forceUnicode]].}}


|s1= [[count]] value
|s1= [[count]] value
Line 44: Line 44:
|s2= condition [[count]] array
|s2= condition [[count]] array


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


|p22= array: [[Array]]
|p22= array: [[Array]]
Line 51: Line 51:
|r2= [[Number]]
|r2= [[Number]]


|x1= <sqf>count [0, 0, 1, 2]; // returns 4
|x1= <sqf>
count units group player; // returns number of units in player group</sqf>
count [0, 0, 1, 2]; // returns 4
count units group player; // returns number of units in player group
</sqf>


|x2= <sqf>private _cnt = { _x == 4 } count [1, 9, 8, 3, 4, 4, 4, 5, 6]; // returns 3
|x2= <sqf>
_cnt = { alive _x } count allUnits; // returns the number of alive units</sqf>
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
</sqf>


|x3= <sqf>private _cnt = count (configFile >> "CfgVehicles");</sqf>
|x3= <sqf>private _cnt = count (configFile >> "CfgVehicles");</sqf>
Line 61: Line 65:
|x4= <sqf>hint str count "japa is the man!"; // 16</sqf>
|x4= <sqf>hint str count "japa is the man!"; // 16</sqf>


|x5= <code>if (count _myHashMap < 1) then { hint "empty hashmap!"; };</code>
|x5= <sqf>hint format ["There are %1 elements in the provided hashmap", count _myHashMap];</sqf>


|seealso= [[apply]] [[select]] [[in]] [[find]] [[countFriendly]] [[countEnemy]] [[countUnknown]] [[countSide]] [[countType]] [[findIf]] [[forceUnicode]]
|seealso= [[apply]] [[select]] [[in]] [[find]] [[countFriendly]] [[countEnemy]] [[countUnknown]] [[countSide]] [[countType]] [[findIf]] [[forceUnicode]]
}}
}}


<dl class="command_description">
{{Note
 
|user= Hardrock
<dt></dt>
|timestamp= 20060803142700
<dd class="notedate">Posted on August 3, 2006 - 14:27</dd>
|text= ''Notes from before the conversion:''<br>
<dt class="note">[[User:Hardrock|hardrock]]</dt>
<dd class="note">''Notes from before the conversion:''<br>
Use this to calculate how many "M16" mags a soldier has left.  
Use this to calculate how many "M16" mags a soldier has left.  
<sqf>{ _x == "M16" } count magazines soldier1;</sqf>
<sqf>{ _x == "M16" } count magazines soldier1;</sqf>
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:  
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:  
<code>{ alive _x } count units group player;</code>
<sqf>{ alive _x } count units group player;</sqf>
or
or
<code>{ alive _x } count units groupname;</code>
<sqf>{ alive _x } count units groupname;</sqf>
</dd>
}}


<dt></dt>
{{Note
<dd class="notedate">Posted on December 15, 2014 - 00:01 (UTC)</dd>
|user= Heeeere's Johnny!
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
|timestamp= 20141215000100
<dd class="note">
|text= ''count'' can be (ab)used for a very fast and simple check if at least one element in an array fulfills a certain condition:
''count'' can be (ab)used for a very fast and simple check if at least one element in an array fulfills a certain condition:
<sqf>
<sqf>if ({ if ('''_x fulfills condition''') exitWith {1}; false } count _array isEqualTo 1) then
if ({ if (/* _x fulfills condition */) exitWith {1}; false } count _array isEqualTo 1) then
{
{
// do whatever here
// do whatever here
};</sqf>
};
</sqf>
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''.<br>
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''.<br>
If no array element fulfills the condition, the ''count'' will be 0 and the if-condition will be ''false''.
If no array element fulfills the condition, the ''count'' will be 0 and the if-condition will be ''false''.
</dd>
}}


<dt><dt>
{{Note
<dd class="notedate">Posted on December 29, 2014 - 21:23 (UTC)</dd>
|user= Killzone_Kid
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
|timestamp= 20141229212300
<dd class="note">
|text= Quit loop at first fulfilled condition (same as above but faster):
Quit loop at first fulfilled condition (same as above but faster):
<sqf>
<sqf>{if (_x == 4) exitWith {
{
// do something when we reach 4
if (_x == 4) exitWith {
}} count [1,2,3,4,5,6];</sqf>
// do something when we reach 4
</dd>
}
} count [1,2,3,4,5,6];
</sqf>
}}


<dt><dt>
{{Note
<dd class="notedate">Posted on January 2, 2015 - 22:32 (UTC)</dd>
|user= Heeeere's Johnny!
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
|timestamp= 20150102223200
<dd class="note">
|text= Using [[exitWith]] inside a '''count''' loop will overwrite the default functionality and make '''count''' return whatever the '''exitWith''' returns:
 
<sqf>
Using [[exitWith]] inside a '''count''' loop will overwrite the default functionality and make '''count''' return whatever the '''exitWith''' returns:
_result = {
<sqf>_result = {
if (_x isEqualTo 3) exitWith { "Hello" }
    if (_x isEqualTo 3) exitWith { "Hello" }
} count [1,2,3,4,5];
} count [1,2,3,4,5];
// _result {{= "Hello"}}</sqf>
// _result = "Hello"
</dd>
</sqf>
}}


<dt><dt>
{{Note
<dd class="notedate">Posted on August 22, 2016 - 19:41 (UTC)</dd>
|user= Ebay
<dt class="note">[[User:Ebay|Ebay]]</dt>
|timestamp= 20160822194100
<dd class="note">
|text= With the alternative syntax each iteration should result in an interior return of bool or nothing. Example:
With the alternative syntax each iteration should result in an interior return of bool or nothing. Example:
<sqf>
<sqf>createDialog "RscFunctionsViewer";
createDialog "RscFunctionsViewer";
{ lbAdd [292901, _x]; } count ["first", "second", "third"];</sqf>
{ lbAdd [292901, _x]; } count ["first", "second", "third"];
</sqf>
[[lbAdd]] returns a number, so this throws "Error Type Number, expected Bool". Tested in A2OA 1.63.131129
[[lbAdd]] returns a number, so this throws "Error Type Number, expected Bool". Tested in A2OA 1.63.131129
</dd>
}}
 
</dl>

Latest revision as of 19:43, 3 September 2024

Hover & click on the images for description

Description

Description:
Can be used to count:
  • The number of elements in an array (returns the already internally known array size)
  • The number of elements in an array matching the condition
  • The number of sub-entries in a config entry
  • Arma 3 logo black.png1.28 The number of characters in an ANSI string
    If Unicode support is desired, see forceUnicode.
Groups:
ArraysStringsConfigHashMap

Syntax

Syntax:
count value
Parameters:
value: 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 the provided array contains different data types, use isEqualTo for item comparison 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:
hint format ["There are %1 elements in the provided hashmap", count _myHashMap];

Additional Information

See also:
apply select in find countFriendly countEnemy countUnknown countSide countType findIf 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
Hardrock - c
Posted on Aug 03, 2006 - 14:27 (UTC)
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: or
{ alive _x } count units groupname;
Heeeere's Johnny! - c
Posted on Dec 15, 2014 - 00:01 (UTC)
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.
Killzone_Kid - c
Posted on Dec 29, 2014 - 21:23 (UTC)
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];
Heeeere's Johnny! - c
Posted on Jan 02, 2015 - 22:32 (UTC)
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"
Ebay - c
Posted on Aug 22, 2016 - 19:41 (UTC)
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