Arma 3 Scripting

forEachReversed

  • Arma 3 v2.14

Command · also in Arrays · Markdown · JSON · original wiki page

Description

Executes the given command(s) on every item of an Array in reversed order, compared to forEach.

Syntax

code forEachReversed array

Parameters:

Returns: 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;

See also

Control Structures · forEach · for · apply · while · select · findIf · count · deleteAt · reverse