isah_voodoo Posted August 31, 2020 Share Posted August 31, 2020 (edited) Hello again, I have a quick question about arrays and foreach using vex if I have a line like this: i[]@pts= primpoints(0, @primnum); for(int i=0; i<len(@pts); i++){ v@pos = point(0, "P", @pts); } So this is basically getting the current point position of each point in the array and storing it in v@pos. My question is , what is the difference between v@pos = point(0, "P", @pts); VS. v@pos = point(0, "P", i); This would still be doing the same thing correct? Is it better practice to do do @pts instead of just i ? Thanks Edited August 31, 2020 by isah_voodoo Quote Link to comment Share on other sites More sharing options...
DonRomano Posted August 31, 2020 Share Posted August 31, 2020 You're doing it it wrong. There is two ways to iterate over an array : vector myarray[]; foreach(int pt; myarray) { vector pos = point(0, "P", pt); } //or for(int i = 0; i < len(myarray); i++) { vector pos = point(0, "P", myarray[i]); } In your code, you need to pass i as the index of the item you want in your array. Here, you're passing the whole array without specifying an index. i[]@pts= primpoints(0, @primnum); //it's more convenient to assign primpoints to a local variable instead of an attribute for(int i = 0; i < len(@pts); i++) { v@pos = point(0, "P", [i]@pts); } Cheers, Quote Link to comment Share on other sites More sharing options...
isah_voodoo Posted September 1, 2020 Author Share Posted September 1, 2020 @DonRomano Thanks for the reply. Did you mean to write: v@pos = point(0, "P", @pts[i]); instead of: v@pos = point(0, "P", [i]@pts); but if I write: v@pos = point(0, "P", i); Isn't this also iterating through each index of all the arrays in the for loop ? Thanks Quote Link to comment Share on other sites More sharing options...
DonRomano Posted September 1, 2020 Share Posted September 1, 2020 1 hour ago, isah_voodoo said: @DonRomano Thanks for the reply. Did you mean to write: v@pos = point(0, "P", @pts[i]); instead of: v@pos = point(0, "P", [i]@pts); but if I write: v@pos = point(0, "P", i); Isn't this also iterating through each index of all the arrays in the for loop ? Thanks v@pos = point(0, "P", i); returns the position of the point i, instead of the point at the index i of your point arrays. Use i as an array index, not as a point number. https://www.sidefx.com/docs/houdini/vex/arrays.html Quote Link to comment Share on other sites More sharing options...
isah_voodoo Posted September 1, 2020 Author Share Posted September 1, 2020 @DonRomano Thanks. Makes sense now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.