Talk:forEach

From Bohemia Interactive Community
Jump to navigation Jump to search

Plural commands in one frame

"Plural commands are executed within one frame, so this command might cause performance loss when used on very large arrays or with very complex commands."

Heh, in this case, you have to explain why this code is functional:

0 spawn { 
    {
        player sideChat str _x; 
        sleep 1 
    } foreach [1,2,3] 
}

Obviously, the facts listed in the article is untrue. we should either make additional comments or remove this statement. DenV 09:25, 5 April 2010 (CEST)

I am not quite sure how your spawned and simple example contradicts the "plural commands" statement.
But nevertheless - I don't see any indication of multiple commands being executed in one frame, so until somebody can post some proof for that statement, I've removed it from the article. --Kronzky 18:58, 20 July 2010 (CEST)


_x is local to each forEach block

"In ArmA2, the variable _x is always local to the foreach block so it is safe to nest foreach commands."

Heh:

_log = [];
{ 
    { 
        _log set [count _log, _x] 
    } foreach ["a", "b", "c"]; 
    _log set [count _log, _x] 
} foreach [1,2,3]; 
_log

in ofp, arma and arma2 this code returns the result:

["a","b","c",1,"a","b","c",2,"a","b","c",3]
And another test:
spawn {
    _c = [];
    _c resize 100000;
    _t = time;
    {
        if ( time != _t ) exitwith {
            hint format ["Facts are facts only when they are supported by facts! time %1 != start time %2", time, _t]
        };
    } foreach _c
}

DenV 11:29, 5 April 2010 (CEST)


Again, I don't see how your example contradicts the statement of _x's locality.
Your first example actually supports the statement, and in the second one you're not even using "_x". (Perhaps, leaving a more meaningful comment than "Heh" would've helped...)
If I execute the following code, I get exactly what I would expect, with nicely separated _x values ("echo" only works in VBS2, but just use globalChat or something similar):
 {
  echo format["[K] L0: %1",_x];
  {
    echo format["[K] L1: %1",_x];
    {
      echo format["[K] L2: %1",_x];
    }forEach _x;
  }forEach _x;
}forEach [ [["a","b"],["aa","bb"]], [[1,2],[11,22]] ];
creates:
L0: [["a","b"],["aa","bb"]]
L1: ["a","b"]
L2: a
L2: b
L1: ["aa","bb"]
L2: aa
L2: bb
L0: [[1,2],[11,22]]
L1: [1,2]
L2: 1
L2: 2
L1: [11,22]
L2: 11
L2: 22
--Kronzky 18:58, 20 July 2010 (CEST)

I wanted to say that this behavior (_x is local) has always been, since the OFP. denisko.redisko