Jump to content
isah_voodoo

array and foreach question

Recommended Posts

Posted (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 by isah_voodoo

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×