forEachReversed: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "(\|[pr][0-9]+ *= *[^- ]*) *- *W([a-z ])" to "$1 - w$2")
m (Some wiki formatting)
 
Line 22: Line 22:
<sqf>
<sqf>
// Will show numbers from 5 to 1 in the chat
// Will show numbers from 5 to 1 in the chat
{ systemChat str _x } forEachReversed [1,2,3,4,5];
{ systemChat str _x } forEachReversed [1, 2, 3, 4, 5];
</sqf>
</sqf>


|x2= Can be useful to walk through array while also deleting items from it, which you couldn't do with [[forEach]] as easily:
|x2= Can be useful to walk through array while also deleting items from it, which you couldn't do with [[forEach]] as easily:
<sqf>
<sqf>
private _array = [1,1,2,2,1,1,2,2];
private _array = [1, 1, 2, 2, 1, 1, 2, 2];
{
{
    if (_x == 2) then { _array deleteAt _forEachIndex };
if (_x == 2) then { _array deleteAt _forEachIndex };
} forEachReversed _array;
} forEachReversed _array;
// _array will be [1,1,1,1] here
// _array will be [1,1,1,1] here
</sqf>
</sqf>
Won't work with [[forEach]] as you might expect it:
 
Will not work with [[forEach]] as you might expect it:
<sqf>
<sqf>
private _array = [1,1,2,2,1,1,2,2];
private _array = [1, 1, 2, 2, 1, 1, 2, 2];
{
{
    if (_x == 2) then { _array deleteAt _forEachIndex };
if (_x == 2) then { _array deleteAt _forEachIndex };
} forEach _array;
} forEach _array;
// _array will be [1,1,2,1,1,2] here
// _array will be [1, 1, 2, 1, 1, 2] here
</sqf>
</sqf>


Line 45: Line 46:
<sqf>
<sqf>
{
{
    if (!alive _x) then { _vehicles deleteAt _forEachIndex };
if (!alive _x) then { _vehicles deleteAt _forEachIndex };
} forEachReversed _vehicles;
} forEachReversed _vehicles;
</sqf>
</sqf>

Latest revision as of 13:42, 12 March 2024

Hover & click on the images for description

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:
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.
// Will show numbers from 5 to 1 in the chat { systemChat str _x } forEachReversed [1, 2, 3, 4, 5];
Example 2:
Can be useful to walk through array while also deleting items from it, which you couldn't do with forEach as easily:
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
Will not work with forEach as you might expect it:
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
Example 3:
Deleting items from array with more complex condition than just comparison.
{ if (!alive _x) then { _vehicles deleteAt _forEachIndex }; } forEachReversed _vehicles;

Additional Information

See also:
Control Structures forEach for apply while select findIf count deleteAt reverse

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