03-01-2015 03:46 PM
Hi,
I'm iterating over multidimentional array using ForEach statement. Let say the array is a 4D array and looks like:
AnArray[0..4][0..3][0..2][0..1]
I want o execute somenthing for every element of this array. To do this I'm using - as I've mentioned - ForEach loop. The point is that the order of the element iterating over is different from the order I expected.
The iteration process starts from iterating first element of first dimention (defined on most lest side - in my example [0..4]). My expectation was that the iteration starts by changing elemnts in dimension defined on most right side - in the example above dimension [0..1].
Is there any chance to change the iteration order?
Thanks.
03-02-2015 08:58 AM
I do not think there is a way to change the order with foreach, however you can always use your own nested loops and loop iteration variables to iterate in any order you like. For example:
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 <= 2; l++)
{
AnArray[i][j][k][l]
}
}
}
}
Another alternative is to change the order of the indices you are using for your array to be the way that will make foreach work out the way you want it to.
Or you might consider posting to the Idea Exchange to propose adding a setting to foreach for this (or something similar): http://forums.ni.com/t5/NI-TestStand-Idea-Exchange/idb-p/teststandideas
Hope this helps,
-Doug
03-02-2015 09:35 AM - edited 03-02-2015 09:36 AM
Hi dug9000,
Yes i was thinking about posting it on Idea forum but first I wanted to reflect my problem on normal forum.
Thanks for your reply.
1. I delibritely don't want to have multiple nested for loops. I want the iteration code be independent from the number of dimensions.
2. Yes, it crossed my mind I can change the order of the dimensions, it will work. But I wanted to check is there other way to do this.
So it looks like I have to post it on the idea forum
Thanks again dug900
03-04-2015 01:56 AM - edited 03-04-2015 02:03 AM
Hi MimiKLM,
Is the following image presenting the order you want to access your rows/columns (marked by values) ? This is the inversed order of array cells stored by TS.
If so, this is quite easy. All you need to do is to multiply all array dimensions and go through your table the same number of times by executing the GetPropertyByOffset method. The difference is that you have to use the arrayOffset (N -1 - loop_index).
Best Regards,