forEachReversed: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{RV|type=command |game1= arma3 |version1= 2.14 |branch= dev |gr1= Arrays |descr= Interates over given array from last index to first index. |s1= code {{subst:PAGENAMEE...") |
No edit summary |
||
Line 1: | Line 1: | ||
{{RV|type=command | {{RV|type=command | ||
|game1= arma3 | |game1=arma3 | ||
|version1= 2.14 | |version1= 2.14 | ||
|branch= dev | |branch= dev | ||
|gr1= Arrays | |gr1=Program Flow | ||
|gr2=Arrays | |||
|descr= | |descr= Executes the given command(s) on every item of an [[Array]] in reversed order, compared to [[forEach]]. | ||
|s1= code [[forEachReversed]] array | |s1= code [[forEachReversed]] array | ||
|p1= code: [[Code]] | |p1= code: [[Code]] to execute on each array item - available variables: | ||
* {{hl|[[Magic Variables#x|_x]]}}: iterated item | * {{hl|[[Magic Variables#x|_x]]}}: iterated item | ||
* {{hl|[[Magic Variables#forEachIndex|_forEachIndex]]}}: item's index | * {{hl|[[Magic Variables#forEachIndex|_forEachIndex]]}}: item's index | ||
Line 20: | Line 21: | ||
|r1= [[Anything]] - Will return the value of last executed statement | |r1= [[Anything]] - Will return the value of last executed statement | ||
|x1= <sqf>{ | |x1= This command lets you easily iterate through an array from last to first element without modifying the array itself with [[reverse]] or using [[for]] loop with negative [[step]]. | ||
<sqf> | |||
// Will show numbers from 5 to 1 in the chat | |||
{ systemChat str _x } forEachReversed [1,2,3,4,5]; | |||
</sqf> | |||
|seealso= [[ | |x2= Can be useful to walk through array while also deleting items from it, which you couldn't do with [[forEach]] as easily: | ||
<sqf> | |||
private _array = [1,1,2,2,1,1,2,2]; | |||
{ | |||
if (_x == 2) then { _array deleteAt _forEachIndex }; | |||
} forEachReversed _array; | |||
// _array will be [1,1,1,1] here | |||
</sqf> | |||
Won't work with [[forEach]] as you might expect it: | |||
<sqf> | |||
private _array = [1,1,2,2,1,1,2,2]; | |||
{ | |||
if (_x == 2) then { _array deleteAt _forEachIndex }; | |||
} forEach _array; | |||
// _array will be [1,1,2,1,1,2] here | |||
</sqf> | |||
|x3= Deleting items from array with more complex condition than just comparison. | |||
<sqf> | |||
{ | |||
if (!alive _x) then { _vehicles deleteAt _forEachIndex }; | |||
} forEachReverse _vehicles; | |||
</sqf> | |||
|seealso= [[Control Structures]] [[forEach]] [[for]] [[apply]] [[while]] [[select]] [[findIf]] [[count]] [[deleteAt]] [[reverse]] | |||
}} | }} |
Revision as of 09:11, 2 April 2023
Description
- Description:
- Executes the given command(s) on every item of an Array in reversed order, compared to forEach.
- Groups:
- Program FlowArrays
Syntax
- Syntax:
- code forEachReversed array
- Parameters:
- code: Code to execute on each array item - available variables:
- _x: iterated item
- _forEachIndex: item's index
- array: Array - The array to iterate over
- Return Value:
- Anything - Will return the value of last executed statement
Examples
- Example 1:
- This command lets you easily iterate through an array from last to first element without modifying the array itself with reverse or using for loop with negative step.
- Example 2:
- Can be useful to walk through array while also deleting items from it, which you couldn't do with forEach as easily:
Won't work with forEach as you might expect it:
- Example 3:
- Deleting items from array with more complex condition than just comparison.
Additional Information
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