ArmA is full of arrays and I also happen to like them. Whether you want to call a script or a function, add items to inventory or just get an object position, sooner or later you will have to deal with arrays. Too many important commands in ArmA will return results in a form of array and there will come time when you need to retrieve arrays of values from config classes.

Arrays are recognised by square brackets.  To define array, assign empty array [] to a variable or fill it with values separated by comas. Values could be numbers, strings, booleans, objects, other variables, you name it. Or even all of the above.

_array = []; //empty array _array = [1,2,3,4,5,6,7,8,9]; //array of numbers _array = ["Bob","Peter","John"]; //array of strings _array = [player,_var,true,var,56,"ok",_this]; //mixed array

Array can contain no elements (empty arrays), 1 element or many elements as well as arrays within arrays. To know how many elements array has you can count then with count command.

_array = []; _result = count _array; //_result is 0 _array = ["hello"]; _result = count _array; //_result is 1 _array = [1,2,3]; _result = count _array; //_result is 3

count command can only count 1st dimension of the array. In order to explore multidimensional array you will have to iterate through every dimension separately.

_array = [[1,2,3],[5,6,7]]; //array contains 2 more arrays [1,2,3] and [5,6,7]

In order to access an element of array you have to use select command plus index of the element you want to access. The very first element of an array (from left to right) has index 0, second element has index 1, third has index 2, etc. Therefore in order to access 15th element of array you will need to use select 14 command. To access the last element of array you can count all elements first then subtract 1 from the total.

_array = ["orange","blue","red","green"]; _result = _array select 1; //_result is "blue" _result = _array select ((count _array) - 1); //_result is "green", last element

To access element of multidimensional array you will need to follow array hierarchy with your selections. To access 3rd element of the 1st array of the 2nd array:

_array = [[[1,2],[3,4]],[[5,6,7],[8,9]]]; _result = ((_array select 1) select 0) select 2; //_result is 7

Now returning to count command. This command has alternative syntax, which allows to execute code on every element of the array. If executed code results in true, this element is counted, otherwise element is not counted. Let’s say you want to know how many times name John appears in the following array.

_array = ["John","Samantha","Billy","Archer","John","John","Peter","Casey"]; _result = {_x == "John"} count _array; //_result is 3

As you no doubt have noticed we used variable _x in our code. count command in this format will iterate through every element of the array and call our code {} for every iteration. _x is special variable which get assigned the element of array currently iterated through. So if our element equals to John it is counted. Also word of warning. When you have mixed array on your hands you have to make sure that comparison you perform is of the right type.

//INCORRECT _array = ["John",23,"Samantha",25,"Billy",67,"Archer",45,"John",23,"John",32]; _result = {_x == "John"} count _array; //script ERROR

The above array will make your script produce an error as soon as it reads 2nd element of the array because our code will expect a string for comparison but will get a number instead. To avoid this you can check the type of element tested beforehand. I am also going to use lazy evaluation syntax because normal evaluation will result in error.

//CORRECT _array = ["John",23,"Samantha",25,"Billy",67,"Archer",45,"John",23,"John",32]; _result = {typeName _x == "STRING" && {_x == "John"}} count _array; //no ERROR

To simply test if an element of certain value is present in the array, use in command. It will return true if such element exists, otherwise false. To test if element is present and if so find its index within array, use find command. find will return -1 if match is not found otherwise it will return the index of the first element it can find that is matching your query.

_array = [1,"two",3,"four",1,"two",3,"four"]; _result = "four" in _array; //_result is true; _result = 4 in _array; //_result is false; _result = _array find "three"; //_result is -1 _result = _array find 3; //_result is 2

It is worth mentioning that both in and find are case sensitive and they also don’t work with nested arrays.

_array = ["one","two","three"]; _result = "TWO" in _array; //_result is false _result = _array find "TWO"; //_result is -1 _array = [[1,2,3],[4,5,6]]; _result = [1,2,3] in _array; //_result is false _result = _array find [1,2,3]; //_result is -1

This is it for the Part 1. In the Part 2 we will look at manipulating and changing arrays.

Enjoy,
KK