deleteAt: Difference between revisions
Jump to navigation
Jump to search
(Deleted incorrect note) |
m (template:command argument fix) |
||
Line 8: | Line 8: | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| 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 |= | | 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 |DESCRIPTION= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| array '''deleteAt''' index |= | | array '''deleteAt''' index |SYNTAX= | ||
|p1= array: [[Array]] |= | |p1= array: [[Array]] |PARAMETER1= | ||
|p2= index: [[Number]] |= | |p2= index: [[Number]] |PARAMETER2= | ||
| [[Anything]] - returns the deleted element |= | | [[Anything]] - returns the deleted element |RETURNVALUE= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
|x1= <code>_arr = [1,2,3]; | |x1= <code>_arr = [1,2,3]; | ||
_rem = _arr [[deleteAt]] 1; | _rem = _arr [[deleteAt]] 1; | ||
[[hint]] [[str]] [_rem, _arr]; //[2,[1,3]]</code> |= | [[hint]] [[str]] [_rem, _arr]; //[2,[1,3]]</code> |EXAMPLE1= | ||
|x2= <code>_arr = [1,2,3]; | |x2= <code>_arr = [1,2,3]; | ||
Line 27: | Line 27: | ||
[[hint]] [[str]] _arr; // [1,2,3] | [[hint]] [[str]] _arr; // [1,2,3] | ||
_arr [[deleteAt]] (_arr [[find]] 2); // existent item | _arr [[deleteAt]] (_arr [[find]] 2); // existent item | ||
[[hint]] [[str]] _arr; // [1,3]</code> |= | [[hint]] [[str]] _arr; // [1,3]</code> |EXAMPLE2= | ||
|x3= <code>_arr = [1,2,3]; | |x3= <code>_arr = [1,2,3]; | ||
Line 33: | Line 33: | ||
[[hint]] [[str]] _arr; // [1,2,3] | [[hint]] [[str]] _arr; // [1,2,3] | ||
_arr [[deleteAt]] (_arr [[findIf]] {(_x [[%]] 2) [[==]] 0}); // Remove first number that's divisible by 2 | _arr [[deleteAt]] (_arr [[findIf]] {(_x [[%]] 2) [[==]] 0}); // Remove first number that's divisible by 2 | ||
[[hint]] [[str]] _arr; // [1,3]</code> |= | [[hint]] [[str]] _arr; // [1,3]</code> |EXAMPLE3= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| [[deleteRange]], [[set]], [[resize]], [[select]], [[in]], [[find]], [[findIf]], [[toArray]], [[toString]], [[forEach]], [[count]], [[pushBack]], [[pushBackUnique]], [[apply]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[splitString]], [[joinString]] |= | | [[deleteRange]], [[set]], [[resize]], [[select]], [[in]], [[find]], [[findIf]], [[toArray]], [[toString]], [[forEach]], [[count]], [[pushBack]], [[pushBackUnique]], [[apply]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[splitString]], [[joinString]] |SEEALSO= | ||
}} | }} |
Revision as of 14:37, 7 April 2019
Description
- Description:
- 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
- Groups:
- Uncategorised
Syntax
- Syntax:
- array deleteAt index
- Parameters:
- array: Array
- index: Number
- Return Value:
- Anything - returns the deleted element
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:
- deleteRangesetresizeselectinfindfindIftoArraytoStringforEachcountpushBackpushBackUniqueapplyappendsortparamparamsarrayIntersectsplitStringjoinString
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
Notes
Bottom Section
- Posted on October 15, 2014 - 16:55 (UTC)
- 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)
- Posted on March 4, 2016 - 15:58 (UTC)
- 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.
- Posted on February 9, 2017 - 22:45 (UTC)
- 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.
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.