VEX - Array question

   350   2   0
User Avatar
Member
139 posts
Joined: March 2016
Offline
Hello-

I'm procedurally creating an array - for the sake of the example let's say:
int a={0,4,4,10}

From this I want to create a new array that is populated with the current element from a, plus the previous element from the new array - if element 0 there would be no addition and its populated like-for-like.

so in the example:
0 = 0 (a[0])
1 = 4 (a[1]+newArray[0])
2 = 8 (a[2]+newArray[1])
3 = 18 (a[3]+newArray[2])

so:
newArray = {0,4,8,18}

My understanding is i cant do this inside a single vex for-loop in an attribute wrangle because newArray doesn't exist to access the previous value until the full loop is complete?

As a workaround I've instead used a For-Loop with feedback node and created the array on each iteration - My question - is this the only way/best to do it or am I missing something obvious?

Thank you.
Edited by Hatchery - Oct. 30, 2024 05:54:42
Love Houdini
User Avatar
Member
218 posts
Joined: May 2017
Offline
Hi, as long as the new array is defined outside the loop scope, you can:

int new_arr[] = {};
int arr[] = {0, 4, 4, 10};

for (int i = 0; i < 4; ++i)
{
    new_arr[i] = arr[i] + new_arr[i - 1];
}

printf('%d \n', new_arr); // {0, 4, 8, 18}
User Avatar
Member
139 posts
Joined: March 2016
Offline
Ah of course, it did feel like I was over complicating it
Thank you for that very much appreciated
Love Houdini
  • Quick Links