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>.
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:
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:
And this is using pretty fast set command instead of slow _arr = _arr + [_val];
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.
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:
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)
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: