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 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.
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.
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.
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:
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.
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.
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.
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.
It is worth mentioning that both in and find are case sensitive and they also don’t work with nested arrays.
This is it for the Part 1. In the Part 2 we will look at manipulating and changing arrays.
Enjoy,
KK
EDIT: Since this post many many more commands for arrays were added to Arma 3, make sure you check see also on BIKI under array commands. Off the top of my head… pushBack, append, reverse, sort, select (expanded), deleteAt, deleteRange. Also arr + arr operation is significantly faster now.