deleteAt: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(see also)
m (2.16 -> 2.18)
 
(96 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma3 |= Game name
|game1= arma3
|version1= 1.32


|1.32|= Game version
|gr1= Arrays


____________________________________________________________________________________________
|gr2= HashMap


| Removes array element at the given index and returns removed element (modifies the original array, just like [[resize]] or [[set]]). |= Description
|descr= Removes array element at the given index and returns removed element (modifies the original array, just like [[resize]] or [[set]]).
____________________________________________________________________________________________
{{Feature|informative| For the main [[Array]] variant, if the index is a negative [[Number]], no error will appear, making it ideal for using with [[find]] (see {{Link|#Example 2}}) or [[findIf]] (see {{Link|#Example 3}}).}}


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


|p1= array: [[Array]] |= Parameter 1
|p1= array: [[Array]]
|p2= index: [[Number]] |= Parameter 2


| [[Anything]] - returns the value of deleted element |= Return value
|p2= index: [[Number]] (see a note in description)
____________________________________________________________________________________________
 
|x1= <code>_arr = [1,2,3];
_rem = _arr [[deleteAt]] 1;
[[hint]] [[str]] [_rem, _arr]; //[2,[1,3]]</code> |= Example 1


____________________________________________________________________________________________
|r1= [[Anything]] - returns the deleted element or [[Nothing]]


| [[deleteRange]], [[set]], [[resize]], [[select]], [[in]], [[find]], [[toArray]], [[toString]], [[forEach]], [[count]], [[pushBack]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[splitString]], [[joinString]] |= See also
|s2= hashMap [[deleteAt]] key


}}
|s2since= arma3 2.02
 
|p21= hashMap: [[HashMap]]
 
|p22= key: [[HashMapKey]]
 
|r2= [[Anything]]
 
|s3= array [[deleteAt]] indexes
 
|s3since= arma3 2.18
 
|p41= array: [[Array]]
 
|p42= indexes: [[Array]] - multiple indexes, negative indexes are supported and would remove elements counting from the end of array (see {{Link|#Example 4}}).


<h3 style="display:none">Notes</h3>
|r3= [[Array]] -  deleted elements in order of user supplied indexes
<dl class="command_description">
</dl>


<h3 style="display:none">Bottom Section</h3>
|x1= <sqf>
_arr = [1,2,3];
_rem = _arr deleteAt 1;
hint str [_rem, _arr]; // [2, [1, 3]]
</sqf>


|x2= <sqf>
_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]
</sqf>


[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
|x3= <sqf>
[[Category:Arma_3:_New_Scripting_Commands_List|{{uc:{{PAGENAME}}}}]]
_arr = [1,2,3];
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
_arr deleteAt (_arr findIf {(_x % 5) == 0}); // Remove first number that's divisible by 5
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
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]
</sqf>


<!-- CONTINUE Notes -->
|x4= <sqf>
<dl class="command_description">
_arr = [1,2,3,4];
<dd class="notedate">Posted on October 15, 2014 - 16:55 (UTC)</dd>
_res = _arr deleteAt [-1, 0]; // Remove the last and the first array element
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
systemchat str [_res, _arr]; // [[4,1],[2,3]]
<dd class="note"> <code>_array deleteAt 0</code>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)
</sqf>
</dd>
</dl>
<!-- DISCONTINUE Notes -->


<!-- CONTINUE Notes -->
|seealso= [[deleteRange]] [[set]] [[resize]] [[select]] [[in]] [[find]] [[findIf]] [[toArray]] [[toString]] [[forEach]] [[count]] [[pushBack]] [[pushBackUnique]] [[apply]] [[append]] [[sort]] [[param]] [[params]] [[arrayIntersect]] [[splitString]] [[joinString]]
<dl class="command_description">
}}
<dd class="notedate">Posted on May 21, 2015 - 10:38 (UTC)</dd>
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
<dd class="note">
Array "shift" implementation using [[deleteAt]], alternative to [[BIS_fnc_arrayShift]]
<code>KK_fnc_shift <nowiki>=</nowiki> {
_this [[deleteAt]] 0
};


// Example
{{Note
arr <nowiki>=</nowiki> [1,2,3];
|user= Heeeere's Johnny!
arr [[call]] KK_fnc_shift; //return of function is 1, arr now is [2,3]</code>
|timestamp= 20141015165500
</dd>
|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>
}}
<!-- DISCONTINUE Notes -->


<!-- CONTINUE Notes -->
{{Note
<dl class="command_description">
|user= Highhead
<dd class="notedate">Posted on May 21, 2015 - 10:44 (UTC)</dd>
|timestamp= 20160304155800
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
|text= Deleting from an array with foreach and _foreachIndex variable is tricky.
<dd class="note">
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.
Array "pop" implementation using [[deleteAt]], alternative to [[BIS_fnc_arrayPop]]
If you delete elements from an array in descending order (using while or for) it will work.
<code>KK_fnc_pop <nowiki>=</nowiki> {
}}
_this [[deleteAt]] ([[count]] _this - 1)
};


// Example
{{Note
arr <nowiki>=</nowiki> [1,2,3];
|user= Igneous01
arr [[call]] KK_fnc_pop; //return of function is 3, arr now is [1,2]</code>
|timestamp= 20170209224500
</dd>
|text= To expand on Highheads comment above - this is because forEach implements iterators to traverse a collection, which are read only by definition.
</dl>
The variable _x is an iterator that points to the current item in the collection. Trying to alter _x will have no effect.
<!-- DISCONTINUE Notes -->
<sqf>
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]
</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.
In short, only use forEach when reading data from an array.
For more info about iterators, see C++ Iterators.
}}
 
{{Note
|user= Freghar
|timestamp= 20210201180600
|text= As you would expect from a simple array implementation,
<sqf>array deleteAt (count array - 1);</sqf>
is virtually zero cost (0.0005ms on an array of 1000000 elements), while
<sqf>array deleteAt 0;</sqf>
is very performance heavy (0.7ms on the same huge array).
}}

Latest revision as of 15:08, 8 February 2024

Hover & click on the images for description

Description

Description:
Removes array element at the given index and returns removed element (modifies the original array, just like resize or set).
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).
Groups:
ArraysHashMap

Syntax 1

Syntax:
array deleteAt index
Parameters:
array: Array
index: Number (see a note in description)
Return Value:
Anything - returns the deleted element or Nothing

Syntax 2

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

Syntax 3

Syntax:
array deleteAt indexes
Parameters:
array: Array
indexes: Array - multiple indexes, negative indexes are supported and would remove elements counting from the end of array (see Example 4).
Return Value:
Array - 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]]

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).