count: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
m (Reverted edits by POLPOX (talk) to last revision by Lou Montana)
Tag: Rollback
 
(91 intermediate revisions by 6 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


|arg= local |Multiplayer Arguments=
|game3= arma1
____________________________________________________________________________________________
|version3= 1.00


| Can be used to count:
|game4= arma2
* the number of elements in array (doesn’t actually count them per se but rather returns already known array size)
|version4= 1.00
* 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"])
|DESCRIPTION=
____________________________________________________________________________________________


| [[count]] array |SYNTAX=
|game5= arma2oa
|version5= 1.50


| p1= array: [[Array]] |Parameter1=
|game6= tkoh
|version6= 1.00


| [[Number]] |RETURNVALUE=
|game7= arma3
____________________________________________________________________________________________
|version7= 0.50


| s2= condition [[count]] array |SYNTAX2=
|gr1= Arrays


| p21= condition: [[Code]] that must return [[true]] for the tested element to be counted. The variable ''[[_x]]'' will contain the currently tested element. |PARAMETER21=
|gr2= Strings


| p22= array: [[Array]] |PARAMETER22=
|gr3= Config


| r2= [[Number]] |RETURNVALUE2=
|gr4= HashMap
____________________________________________________________________________________________


| s3= [[count]] configname |SYNTAX3=
|descr= 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
* {{GVI|arma3|1.28|size= 0.75}} The number of characters in an ANSI string {{Feature|informative|If Unicode support is desired, see [[forceUnicode]].}}


| p41= configname: [[Config]] |Parameter41=
|s1= [[count]] value


| r3= [[Number]] |RETURNVALUE3=
|p1= value: [[Array]], [[String]], [[Config]] or [[HashMap]]
____________________________________________________________________________________________
| s4= [[count]] string |SYNTAX4=


| p61= string: [[String]] |Parameter61=
|r1= [[Number]]


| r4= [[Number]] |RETURNVALUE4=
|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
 
{{Feature|arma3|If array contains different data types, use [[isEqualTo]] instead of [[==]]!}}
|x1= <code>_cnt = [[count]] [0, 0, 1, 2]; {{cc|returns 4}}
_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}}
|p22= array: [[Array]]
_cnt = {[[alive]] [[_x]]} [[count]] [[allUnits]]; {{cc|returns the number of alive units}}</code> |EXAMPLE2=


|x3= <code>_cnt = [[count]] ([[configFile]] [[config greater greater name|>>]] "CfgVehicles");</code> |EXAMPLE3=
|r2= [[Number]]


|x4= <code>[[hint]] [[str]] [[count]] "japa is the man!"; {{cc|16}}</code> |EXAMPLE4=
|x1= <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
_cnt = { alive _x } count allUnits; // returns the number of alive units</sqf>


| [[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]]|SEEALSO=
|x3= <sqf>private _cnt = count (configFile >> "CfgVehicles");</sqf>


|x4= <sqf>hint str count "japa is the man!"; // 16</sqf>
|x5= <sqf>if (count _myHashMap < 1) then { hint "empty hashmap!"; };</sqf>
|seealso= [[apply]] [[select]] [[in]] [[find]] [[countFriendly]] [[countEnemy]] [[countUnknown]] [[countSide]] [[countType]] [[findIf]] [[forceUnicode]]
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
<dl class="command_description">
|user= Hardrock
<!-- Note Section BEGIN -->
|timestamp= 20060803142700
<dd class="notedate">Posted on April 28, 2007 - 13:49
|text= ''Notes from before the conversion:''<br>
<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>
For example, the following code will created an error, since the elements are of different types (object, number, string):
<code>_arr = [ [[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>
 
<dd class="notedate">Posted on August 3, 2006 - 14:27
<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 == "M16" } [[count]] [[magazines]] soldier1;</code>
<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>
<!-- Note Section END -->
}}
</dl>
 
<h3 style="display:none">Bottom Section</h3>
 
 
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.46|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.96|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands OFP 1.99|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Armed Assault|{{uc:{{PAGENAME}}}}]]
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 2|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands Take On Helicopters|{{uc:{{PAGENAME}}}}]]


<!-- CONTINUE Notes -->
{{Note
<dl class="command_description">
|user= Heeeere's Johnny!
<dd class="notedate">Posted on December 15, 2014 - 00:01 (UTC)</dd>
|timestamp= 20141215000100
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
|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:
<dd class="note">
<sqf>if ({ if (/* _x fulfills condition */) exitWith {1}; false } count _array isEqualTo 1) then
''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]]
{
{
{{cc|do whatever here}}
// do whatever here
};</code>
};</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>
}}


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


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


Using [[exitWith]] inside a '''count''' loop will overwrite the default functionality and make '''count''' return whatever the '''exitWith''' returns:
{{Note
<code>_result = {
|user= Ebay
    if (_x [[isEqualTo]] 3) [[exitWith]] { "Hello" }
|timestamp= 20160822194100
} [[count]] [1,2,3,4,5];
|text= With the alternative syntax each iteration should result in an interior return of bool or nothing. Example:
{{cc|_result {{=}} "Hello"}}</code>
<sqf>
</dd>
createDialog "RscFunctionsViewer";
 
{ lbAdd [292901, _x]; } count ["first", "second", "third"];
<dd class="notedate">Posted on August 22, 2016 - 19:41 (UTC)</dd>
</sqf>
<dt class="note">[[User:Ebay|Ebay]]</dt>
<dd class="note">
With the alternative syntax each iteration should result in an interior return of bool or nothing. Example:
<code>[[createDialog]] "RscFunctionsViewer";
{ [[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>
}}
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 12:49, 4 December 2023

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