count: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (minor wording addition in my comment) |
||
Line 107: | Line 107: | ||
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt> | <dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt> | ||
<dd class="note"> | <dd class="note"> | ||
''count'' can be (ab)used for a very fast 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 {true}; false} count _array isEqualTo 1) then | <code>if({if('''_x fulfills condition''') exitWith {true}; false} count _array isEqualTo 1) then | ||
{ | { |
Revision as of 01:02, 15 December 2014
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 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 4
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
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
- Alternatively, to avoid the error use isEqualTo instead of ==. --KK
_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 {true}; 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.
Categories:
- Scripting Commands
- Introduced with Operation Flashpoint version 1.00
- Operation Flashpoint: New Scripting Commands
- Operation Flashpoint: Scripting Commands
- Command Group: Uncategorised
- Scripting Commands OFP 1.99
- Scripting Commands OFP 1.96
- Scripting Commands OFP 1.46
- Scripting Commands ArmA
- Command Group: Variables
- Scripting Commands ArmA2
- Scripting Commands Arma 3
- Scripting Commands Take On Helicopters