deleteAt: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<code>([a-zA-Z0-9_]+) +\[\[([a-zA-Z0-9_]+)\]\] +([a-zA-Z0-9_]+);?<\/code>" to "<sqf>$1 $2 $3;</sqf>")
m (Some wiki formatting)
Line 8: Line 8:
|gr2= HashMap
|gr2= HashMap


|descr= Removes array element at the given index and returns removed element (modifies the original array, just like [[resize]] or [[set]]). Will not issue error when -1 index is passed making it ideal for using with [[find]](see Example 2) or [[findIf]](see Example 3) command
|descr= Removes array element at the given 0-based index and returns removed element (modifies the original array, just like [[resize]] or [[set]]).
{{Feature|informative|This command will not issue error when -1 index is passed making it ideal for using with [[find]] (see {{HashLink|#Example 2}}) or [[findIf]] (see {{HashLink|#Example 3}}).}}


|s1= array [[deleteAt]] index
|s1= array [[deleteAt]] index


|p1= array: [[Array]]
|p1= array: [[Array]]
|p2= index: [[Number]]
|p2= index: [[Number]]


|r1= [[Anything]] - returns the deleted element
|r1= [[Anything]] - returns the deleted element


|s2= hashMap [[deleteAt]] key
|s2= hashMap [[deleteAt]] key
|s2since= arma3 2.02


|p21= hashMap: [[HashMap]]
|p21= hashMap: [[HashMap]]
|p22= key : [[HashMapKey]]
 
|p22= key: [[HashMapKey]]


|r2= [[Anything]]
|r2= [[Anything]]


|x1= <code>_arr = [1,2,3];
|x1= <sqf>
_rem = _arr [[deleteAt]] 1;
_arr = [1,2,3];
[[hint]] [[str]] [_rem, _arr]; //[2,[1,3]]</code>
_rem = _arr deleteAt 1;
hint str [_rem, _arr]; // [2, [1, 3]]
</sqf>


|x2= <code>_arr = [1,2,3];
|x2= <sqf>
_arr [[deleteAt]] (_arr [[find]] 0); // non existent item
_arr = [1,2,3];
[[hint]] [[str]] _arr; // [1,2,3]
_arr deleteAt (_arr find 0); // non existent item
_arr [[deleteAt]] (_arr [[find]] 2); // existent item
hint str _arr; // [1,2,3]
[[hint]] [[str]] _arr; // [1,3]</code>
_arr deleteAt (_arr find 2); // existent item
hint str _arr; // [1,3]
</sqf>


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


|seealso= [[deleteRange]] [[set]] [[resize]] [[select]] [[in]] [[find]] [[findIf]] [[toArray]] [[toString]] [[forEach]] [[count]] [[pushBack]] [[pushBackUnique]] [[apply]] [[append]] [[sort]] [[param]] [[params]] [[arrayIntersect]] [[splitString]] [[joinString]]
|seealso= [[deleteRange]] [[set]] [[resize]] [[select]] [[in]] [[find]] [[findIf]] [[toArray]] [[toString]] [[forEach]] [[count]] [[pushBack]] [[pushBackUnique]] [[apply]] [[append]] [[sort]] [[param]] [[params]] [[arrayIntersect]] [[splitString]] [[joinString]]
}}
}}


{{Note
|user= Heeeere's Johnny!
|timestamp= 20141015165500
|text= <sqf>_array deleteAt 0;</sqf> is almost 60x faster than <sqf>_array = _array - [_array select 0]</sqf> (Tested with an array of 10.000 strings, iterating through it using a for-from-to-do loop)
}}


<dl class="command_description">
{{Note
 
|user= Highhead
<dt></dt>
|timestamp= 20160304155800
<dd class="notedate">Posted on October 15, 2014 - 16:55 (UTC)</dd>
|text= Deleting from an array with foreach and _foreachIndex variable is tricky.
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
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.
<dd class="note"> <sqf>_array deleteAt 0;</sqf>is almost 60x faster than<code>_array = _array - [_array select 0]</code>(Tested with an array of 10.000 strings, iterating through it using a for-from-to-do loop)
</dd>
 
<dt><dt>
<dd class="notedate">Posted on March 4, 2016 - 15:58 (UTC)</dd>
<dt class="note">[[User:Highhead|Highhead]]</dt>
<dd class="note">
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.
If you delete elements from an array in descending order (using while or for) it will work.
</dd>
}}


<dt><dt>
{{Note
<dd class="notedate">Posted on February 9, 2017 - 22:45 (UTC)</dd>
|user= Igneous01
<dt class="note">[[User:Igneous01|Igneous01]]</dt>
|timestamp= 20170209224500
<dd class="note">
|text= To expand on Highheads comment above - this is because forEach implements iterators to traverse a collection, which are read only by definition.  
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.  
The variable _x is an iterator that points to the current item in the collection. Trying to alter _x will have no effect.  
<code>ARRAY = [1,2,3,4,5,6,7,8];
<sqf>
{ _x = 2; } forEach ARRAY
private _array = [1,2,3,4,5,6,7,8];
// ARRAY is still [1,2,3,4,5,6,7,8]</code>
{ _x = 2; } forEach _array
// _array is still [1,2,3,4,5,6,7,8]
</sqf>
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.  
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.
In short, only use forEach when reading data from an array.
For more info about iterators, see C++ Iterators.
For more info about iterators, see C++ Iterators.
</dd>
}}


<dt><dt>
{{Note
<dd class="notedate">Posted on October 3, 2019 - 13:16 (UTC)</dd>
|user= Freghar
<dt class="note">[[User:Tankbuster|Tankbuster]]</dt>
|timestamp= 20210201180600
<dd class="note">
|text= As you would expect from a simple array implementation,
The index you pass to this command is zero based. The first element is number 0 and the second is 1 etc
<sqf>array deleteAt (count array - 1);</sqf>
</dd>
 
<dt></dt>
<dd class="notedate">Posted on February 1, 2021 - 18:06 (UTC)</dd>
<dt class="note">[[User:Freghar|Freghar]]</dt>
<dd class="note">
As you would expect from a simple array implementation,
<code>array deleteAt (count array - 1);</code>
is virtually zero cost (0.0005ms on an array of 1000000 elements), while
is virtually zero cost (0.0005ms on an array of 1000000 elements), while
<code>array deleteAt 0;</code>
<sqf>array deleteAt 0;</sqf>
is very performance heavy (0.7ms on the same huge array).
is very performance heavy (0.7ms on the same huge array).
</dd>
}}
 
</dl>

Revision as of 14:38, 5 May 2022

Hover & click on the images for description

Description

Description:
Removes array element at the given 0-based index and returns removed element (modifies the original array, just like resize or set).
This command will not issue error when -1 index is passed making it ideal for using with find (see Example 2) or findIf (see Example 3).
Groups:
ArraysHashMap

Syntax

Syntax:
array deleteAt index
Parameters:
array: Array
index: Number
Return Value:
Anything - returns the deleted element

Alternative Syntax

Syntax:
hashMap deleteAt key
Parameters:
hashMap: HashMap
key: HashMapKey
Return Value:
Anything

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]

Additional Information

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

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Heeeere's Johnny! - c
Posted on Oct 15, 2014 - 16:55 (UTC)
_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 - c
Posted on Mar 04, 2016 - 15:58 (UTC)
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 - c
Posted on Feb 09, 2017 - 22:45 (UTC)
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 - c
Posted on Feb 01, 2021 - 18:06 (UTC)
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).