---
name: "count"
type: command
status: ok
groups: ["Arrays", "Strings", "Config", "HashMap"]
arma3_since: "0.50"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/count
revid: 377184
updated: 2026-01-01T10:51:01Z
---

# count

*Command · Arrays, Strings, Config, HashMap · Arma 3 v0.50 · introduced in Operation Flashpoint v1.00*

## 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 v1.28 The number of characters in an ANSI string 

> [!NOTE]
> If Unicode support is desired, see [forceUnicode](../commands/forceUnicode.md).

## Syntax

### Syntax 1

```sqf
count value
```

**Parameters:**

- `value`: [String](../topics/String.md), [Config](../topics/Config.md), [HashMap](../topics/HashMap.md) or [Array](../topics/Array.md) of [Anything](../topics/Anything.md)

**Returns:** [Number](../topics/Number.md)

### Syntax 2

```sqf
condition count array
```

**Parameters:**

- `condition`: [Code](../topics/Code.md) - condition that must return [true](../commands/true.md) for the tested element to be counted. The variable `_x` will contain the currently tested element

  > [!NOTE]
  > **Arma 3:** If the provided array contains different data types, use [isEqualTo](../commands/isEqualTo.md) for item comparison instead of ==.
- `array`: [Array](../topics/Array.md) of [Anything](../topics/Anything.md)

**Returns:** [Number](../topics/Number.md)

## Examples

### Example 1

```sqf
count [0, 0, 1, 2]; // returns 4
count units group player; // returns number of units in player group
```

### Example 2

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

### Example 3

```sqf
private _cnt = count (configFile >> "CfgVehicles");
```

### Example 4

```sqf
hint str count "japa is the man!"; // 16
```

### Example 5

```sqf
hint format ["There are %1 elements in the provided hashmap", count _myHashMap];
```

## See also

[apply](../commands/apply.md) · [select](../commands/select.md) · [in](../commands/in.md) · [find](../commands/find.md) · [countFriendly](../commands/countFriendly.md) · [countEnemy](../commands/countEnemy.md) · [countUnknown](../commands/countUnknown.md) · [countSide](../commands/countSide.md) · [countType](../commands/countType.md) · [findIf](../commands/findIf.md) · [forceUnicode](../commands/forceUnicode.md)

## Community notes

**Hardrock** — 2006-08-03

*Notes from before the conversion:*

Use this to calculate how many "M16" mags a soldier has left.

```sqf
{ _x == "M16" } count magazines soldier1;
```

Take care when using count to determine how many units are left alive in a group: count units [group](../commands/group.md) [player](../commands/player.md) 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:

```sqf
{ alive _x } count units group player;
```

or

```sqf
{ alive _x } count units groupname;
```

---

**Heeeere's Johnny!** — 2014-12-15

*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
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** — 2014-12-29

Quit loop at first fulfilled condition (same as above but faster):

```sqf
{
    if (_x == 4) exitWith {
        // do something when we reach 4
    }
} count [1,2,3,4,5,6];
```

---

**Heeeere's Johnny!** — 2015-01-02

Using [exitWith](../commands/exitWith.md) 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"
```

---

**Ebay** — 2016-08-22

With the alternative syntax each iteration should result in an interior return of bool or nothing. Example:

```sqf
createDialog "RscFunctionsViewer";
{ lbAdd [292901, _x]; } count ["first", "second", "third"];
```

[lbAdd](../commands/lbAdd.md) returns a number, so this throws "Error Type Number, expected Bool". Tested in A2OA 1.63.131129

<sub>Source: [BI Community Wiki — count](https://community.bistudio.com/wiki/count) (revision 377184).</sub>
