And here we are, back to arrays again. I’m thinking this is because arrays are too fundamental to ArmA scripting language. And for the same reason I think few things about arrays should be improved to give more flexibility to the end user. I’m going to talk about the improvement idea a bit later.

So, the resize command. It will count however many elements you specified from the beginning of the array and hack off the rest. So if your array is 5 elements long, resize 3 will shorten the array to only 3 elements. If you specify the size larger than current array size, it will extend the array and fill extended positions with <null>.

_arr = [1,2,3,4,5]; _arr resize 3; //[1,2,3] _arr resize 5; //[1,2,3,<null>,<null>]

Note how the command doesn’t return anything but instead alters the array itself. A word of warning though. Just like set command, it will alter the source array even if you give it only reference to the source. So make a copy if you don’t want source altered:

_arr = [1,2,3,4,5]; _arr2 = _arr; _arr2 resize 3; hint str _arr; //[1,2,3] _arr = [1,2,3,4,5]; _arr2 = +_arr; //copy array instead _arr2 resize 3; hint str _arr; //[1,2,3,4,5]

So what is so good about this command? It is fast. Let me give you practical example. Let’s say we have a 100 elements array but we need only first 50 elements from it. If not for resize, we would have needed to loop through the first 50 elements adding them into a new array. Why is this relevant? I’m getting to it. Lets compare the speed first:

arr = []; for "_i" from 0 to 99 do {arr set [_i, 0]}; arr2 = []; [ 'for "_i" from 0 to 49 do { arr2 set [_i, arr select _i] }' ] call BIS_fnc_codePerformance;

slowresize

And this is using pretty fast set command instead of slow _arr = _arr + [_val];

arr = []; for "_i" from 0 to 99 do {arr set [_i, 0]}; [ 'arr2 = +arr; arr2 resize 50' ] call BIS_fnc_codePerformance;

speedtestresize

Speed boost x13! And this is just on the pathetic 50 elements array. But we do have resize command available already, so what is the point? The point is, what if you need to do the same but from the end of the array instead of the beginning? The truth is there is no alternative but iterating through an array like we did in the first example. The resize command works only from the beginning of the array. This is why I made this support ticket on ArmA 3 feedback tracker. The idea is to let resize work in reverse if negative size is supplied (currently not supported).

Everything below is not currently possible. So I’m just going to go ahead and give you a bunch of examples showing how much better it could have been if this feature was implemented.

//remove 2 elements from the beginning of array _arr = [1,2,3,4,5]; _arr resize -(count _arr - 2); //[3,4,5] //insert [0,0,0] into array at position 3 _arr = [1,1,1,1,1]; _arr1 = +_arr; _arr1 resize 3; _arr2 = +_arr; _arr2 resize -(count _arr - 3); _arr = _arr1 + [0,0,0] + _arr2; //[1,1,1,0,0,0,1,1] //grab portion of array from position 3 to the end of array _arr = ["pears","apples","apricots","cherries","strawberries"]; _arr resize -(count _arr - 3); //["cherries","strawberries"] //grab 3 elements of array starting from position 1 _arr = ["pears","apples","apricots","cherries","strawberries"]; _arr resize -(count _arr - 1); _arr resize 3; //["apples","apricots","cherries"]

And the best bit for the last. As you already know string operation support in SQF is really poor. A simple substring or substr function that exists in many languages is absent in SQF. So I made my own substr function just to demonstrate how beneficial improved resize could be:

fnc_substr = { //substring = [string, from, length] call fnc_substr; private "_arr"; _arr = toArray (_this select 0); _arr resize -(count _arr - (_this select 1)); _arr resize (_this select 2); toString _arr }; hint (["hello world", 3, 5] call fnc_substr); //lo wo

Do you remember the improved inString function? This is how it would look if I were to use the above fnc_substr (Note the estimated additional speed boost x4.5 without the use of any RegEx)

inString = [ "112123123412345123456123456712345678123456789", 36, 9 ] call fnc_substr == "123456789";

fastsubstr

I’ve also been told these kinds of improvements are rarely considered as priority by BIS. So if you like the idea and benefits this might bring, please upvote and comment to keep the ticket on top.

Enjoy,
KK

EDIT: As rightfully pointed by ChuangTseu, the resize test results were skewed, so I corrected it. This was 2 years ago and since then many useful commands have appeared, I just physically cannot go back to every post and update it, so bear this in mind. But as this one was brought up, here is even better way of getting first 50 elements from an array:

arrayselectspeedtest