Arma 3 Scripting

deleteAt

  • Arma 3 v1.32

Command · also in HashMap · Markdown · JSON · original wiki page

Description

Removes array element at the given index and returns removed element (modifies the original array, just like resize or set).

Note

For the main Array variant, if the index is a negative Number, no error will appear, making it ideal for using with find (see #Example 2) or findIf (see #Example 3).

Syntax

Syntax 1

array deleteAt index

Parameters:

Returns: Anything or Nothing - returns the deleted element if one existed

Syntax 2

since arma3 2.02

hashMap deleteAt key

Parameters:

Returns: Anything

Syntax 3

since arma3 2.18

array deleteAt indexes

Parameters:

Returns: Array of Anything - deleted elements in order of user supplied indexes

Examples

Example 1

_arr = [1,2,3];
_rem = _arr deleteAt 1;
hint str [_rem, _arr]; // [2, [1, 3]]

Example 2

_arr = [1,2,3];
_arr deleteAt (_arr find 0); // non existent item
hint str _arr; // [1,2,3]
_arr deleteAt (_arr find 2); // existent item
hint str _arr; // [1,3]

Example 3

_arr = [1,2,3];
_arr deleteAt (_arr findIf {(_x % 5) == 0}); // Remove first number that's divisible by 5
hint str _arr; // [1,2,3]
_arr deleteAt (_arr findIf {(_x % 2) == 0}); // Remove first number that's divisible by 2
hint str _arr; // [1,3]

Example 4

_arr = [1,2,3,4];
_res = _arr deleteAt [-1, 0]; // Remove the last and the first array element
systemchat str [_res, _arr]; // [[4,1],[2,3]]

See also

deleteRange · set · resize · select · in · find · findIf · toArray · toString · forEach · count · pushBack · pushBackUnique · apply · append · sort · param · params · arrayIntersect · splitString · joinString

Community notes

Heeeere's Johnny!

_array deleteAt 0;

is almost 60x faster than

_array = _array - [_array select 0]

(Tested with an array of 10.000 strings, iterating through it using a for-from-to-do loop)

Highhead

Deleting from an array with foreach and _foreachIndex variable is tricky. The array is being altered, the _foreachIndex won't keep up and other elements in the array will be skipped and in worst case not being deleted. If you delete elements from an array in descending order (using while or for) it will work.

Igneous01

To expand on Highheads comment above - this is because forEach implements iterators to traverse a collection, which are read only by definition. The variable _x is an iterator that points to the current item in the collection. Trying to alter _x will have no effect.

private _array = [1,2,3,4,5,6,7,8];
{ _x = 2; } forEach _array
// _array is still [1,2,3,4,5,6,7,8]

When trying to use deleteAt inside forEach, the behaviour would be undefined as you are invalidating the iterator reference, and it will not know how to traverse to the next element. In short, only use forEach when reading data from an array. For more info about iterators, see C++ Iterators.

Freghar

As you would expect from a simple array implementation,

array deleteAt (count array - 1);

is virtually zero cost (0.0005ms on an array of 1000000 elements), while

array deleteAt 0;

is very performance heavy (0.7ms on the same huge array).