NI TestStand Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
MimiKLM

Iterating multidimensional array using ForEach loop

Status: New

Hi,

 

Now, when we iterate over any multidimensional array, in this example let it to be a 4D array, using ForEach loop the order of iterating is bit surprising.

For example for the array like defined like that
 
AnArray[0..4][0..3][0..2][0..1]
 
The order of iteration is like that the loop start iterating elements from first dimension first [0..4], then from second [0..3], third [0..2] and fourth [0..1]. So from left to right, in shorthand.
 
For me this order seems to be bit surprising. I'd expect rather the ForEach loop will start iterating from right to left as it'd be if we replace ForEach loop with four nested For loops:
 
for (int i = 0; i <= 4; i++)
{
    for(int j = 0; j <= 3; j++)
    {
        for(int k = 0; k <= 2; k++)
        {
            for(int l = 0; l <= 1; l++)
            {
                AnArray[i][j][k][l]
            }
        }
   }
}

 

 
There are some advantages using ForEach loop over ordinary For loop. For example ForEach loop is dimension independed.
 
Is there any chance developers can have an option or flag which change the order of iteration for ForEach loop from left to right to right to left?
 
PS: I've started the topic in normal forum here, but after dug9000 input, I'm posting it here.