count: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "|= Game name" to "|Game name=")
Line 17: Line 17:
____________________________________________________________________________________________
____________________________________________________________________________________________


| '''count''' array |SYNTAX=
| [[count]] array |SYNTAX=


| p1= array: [[Array]] |= Parameter
| p1= array: [[Array]] |Parameter1=


| [[Number]] |RETURNVALUE=
| [[Number]] |RETURNVALUE=
____________________________________________________________________________________________
____________________________________________________________________________________________


| s2= condition '''count''' array |SYNTAX=
| s2= condition [[count]] array |SYNTAX2=


| 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. |PARAMETER1=
| p21= condition: [[Code]] that must return [[true]] for the tested element to be counted. The variable ''[[_x]]'' will contain the currently tested element. |PARAMETER21=


| p22= array: [[Array]] |PARAMETER2=
| p22= array: [[Array]] |PARAMETER22=


| r2= [[Number]] |RETURNVALUE=
| r2= [[Number]] |RETURNVALUE2=
____________________________________________________________________________________________


____________________________________________________________________________________________
| s3= [[count]] configname |SYNTAX3=
| s3= '''count''' configname |SYNTAX=


| p41= configname: [[Config]] |= Parameter
| p41= configname: [[Config]] |Parameter41=


| r3= [[Number]] |RETURNVALUE=
| r3= [[Number]] |RETURNVALUE3=
____________________________________________________________________________________________
____________________________________________________________________________________________
| s4= '''count''' string |SYNTAX=
| s4= [[count]] string |SYNTAX4=


| p61= string: [[String]] |= Parameter
| p61= string: [[String]] |Parameter61=


| r4= [[Number]] |RETURNVALUE=
| r4= [[Number]] |RETURNVALUE4=


____________________________________________________________________________________________
____________________________________________________________________________________________
   
   
|x1= <code>_cnt = [[count]] [0,0,1,2]; // returns 4
|x1= <code>_cnt = [[count]] [0, 0, 1, 2]; {{cc|returns 4}}
_cnt = [[count]] [[units]] [[group]] [[player]]; // returns number of units in player group</code> |EXAMPLE1=
_cnt = [[count]] [[units]] [[group]] [[player]]; {{cc|returns number of units in player group}}</code> |EXAMPLE1=


|x2= <code>_cnt = {[[_x]] == 4} [[count]] [1, 9, 8, 3, 4, 4, 4, 5, 6]; {{cc|returns 3}}
_cnt = {[[alive]] [[x]]} [[count]] [[allUnits]]; {{cc|returns the number of alive units}}</code> |EXAMPLE2=


|x2= <code>_cnt = {[[Magic Variables|_x]] == 4} [[count]] [1,9,8,3,4,4,4,5,6]; // returns 3
|x3= <code>_cnt = [[count]] ([[configFile]] [[config greater greater name|>>]] "CfgVehicles");</code> |EXAMPLE3=
_cnt = {[[alive]] [[Magic Variables|_x]]} [[count]] [[allUnits]]; // returns the number of alive units</code> |EXAMPLE2=


|x3= <code>_cnt = [[count]] ([[configFile]] [[gtgt|>>]] "CfgVehicles");</code> |EXAMPLE3=
|x4= <code>[[hint]] [[str]] [[count]] "japa is the man!"; {{cc|16}}</code> |EXAMPLE4=
 
|x4= <code>[[hint]] [[str]] [[count]] "japa is the man!"; //16</code> |EXAMPLE4=


________________________________________________________________________________________
________________________________________________________________________________________
Line 69: Line 68:
<!-- Note Section BEGIN -->
<!-- Note Section BEGIN -->
<dd class="notedate">Posted on April 28, 2007 - 13:49
<dd class="notedate">Posted on April 28, 2007 - 13:49
<dt class="note">'''[[User:Kronzky|Kronzky]]'''<dd class="note">
<dt class="note">[[User:Kronzky|Kronzky]]
<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>
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):
For example, the following code will created an error, since the elements are of different types (object, number, string):
Line 76: Line 76:
This one, on the other hand, where all elements are strings, just like the tested element, will return the correct result of 1:
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>
<code>_arr = ["one","two","three"]; {_x == "one"} [[count]] _arr;</code>


<dd class="notedate">Posted on August 3, 2006 - 14:27
<dd class="notedate">Posted on August 3, 2006 - 14:27
<dt class="note">'''[[User:Hardrock|hardrock]]'''<dd class="note">''Notes from before the conversion:''
<dt class="note">[[User:Hardrock|hardrock]]
<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.  
<code>{_x <nowiki>==</nowiki> "M16"} [[count]] [[magazines]] soldier1;</code>
<code>{ _x == "M16" } [[count]] [[magazines]] soldier1;</code>
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>
<code>{ [[alive]] _x } [[count]] [[units]] [[group]] [[player]];</code>
or
or
<code>{[[alive]] _x} [[count]] [[units]] groupname;</code>
<code>{ [[alive]] _x } [[count]] [[units]] groupname;</code>
<!-- Note Section END -->
<!-- Note Section END -->
</dl>
</dl>
Line 92: Line 92:




[[Category:Scripting Commands|COUNT]]
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.99|COUNT]]
[[Category:Scripting Commands OFP 1.46|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.96|COUNT]]
[[Category:Scripting Commands OFP 1.96|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.46|COUNT]]
[[Category:Scripting Commands OFP 1.99|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA|COUNT]]
[[Category:Scripting Commands ArmA|{{uc:{{PAGENAME}}}}]]
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
Line 108: Line 108:
<dd class="note">
<dd class="note">
''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:
<code>if({if('''_x fulfills condition''') exitWith {1}; false} '''count''' _array isEqualTo 1) then
<code>[[if]] ({ [[if]] ('''_x fulfills condition''') exitWith {1}; false } [[count]] _array [[isEqualTo]] 1) [[then]]
{
{
    //do whatever here
{{cc|do whatever here}}
};</code>
};</code>
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>
</dd>
</dl>
<!-- DISCONTINUE Notes -->


<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on December 29, 2014 - 21:23 (UTC)</dd>
<dd class="notedate">Posted on December 29, 2014 - 21:23 (UTC)</dd>
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
<dd class="note">
<dd class="note">
Quit loop at first fulfilled condition (same as above but faster):
Quit loop at first fulfilled condition (same as above but faster):
<code>0 <nowiki>=</nowiki> {[[if]] (_x <nowiki>==</nowiki> 4) [[exitWith]] {
<code>{[[if]] (_x == 4) [[exitWith]] {
//do something when we reach 4
{{cc|do something when we reach 4}}
<nowiki>}}</nowiki> [[count]] [1,2,3,4,5,6];</code>
<nowiki>}}</nowiki> [[count]] [1,2,3,4,5,6];</code>
</dd>
</dd>
</dl>
<!-- DISCONTINUE Notes -->


<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on January 2, 2015 - 22:32 (UTC)</dd>
<dd class="notedate">Posted on January 2, 2015 - 22:32 (UTC)</dd>
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
Line 139: Line 131:
Using [[exitWith]] inside a '''count''' loop will overwrite the default functionality and make '''count''' return whatever the '''exitWith''' returns:
Using [[exitWith]] inside a '''count''' loop will overwrite the default functionality and make '''count''' return whatever the '''exitWith''' returns:
<code>_result = {
<code>_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"</code>
{{cc|_result {{=}} "Hello"}}</code>
</dd>
</dd>
</dl>
<!-- DISCONTINUE Notes -->


<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on August 22, 2016 - 19:41 (UTC)</dd>
<dd class="notedate">Posted on August 22, 2016 - 19:41 (UTC)</dd>
<dt class="note">[[User:Ebay|Ebay]]</dt>
<dt class="note">[[User:Ebay|Ebay]]</dt>
<dd class="note">
<dd class="note">
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:
<code>createDialog "RscFunctionsViewer";
<code>[[createDialog]] "RscFunctionsViewer";
{ lbAdd [292901,_x]; } count ["first","second","third"];</code>
{ [[lbAdd]] [292901, _x]; } [[count]] ["first", "second", "third"];</code>
[[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>
</dd>
</dl>
</dl>
<!-- DISCONTINUE Notes -->
<!-- CONTINUE Notes -->
<!-- DISCONTINUE Notes -->
<!-- DISCONTINUE Notes -->

Revision as of 22:47, 2 September 2019

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:
setresizepushBackpushBackUniqueapplyselectreverseselectinfindtoArraytoStringforEachdeleteAtdeleteRangeappendsortparamparamsarrayIntersectcountFriendlycountEnemycountUnknowncountSidecountTypesplitStringjoinStringfindIf

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

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