Jump to content

array and foreach question


isah_voodoo

Recommended Posts

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
Link to comment
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,

Link to comment
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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...