Jump to content
Sign in to follow this  
BenWall

Handling array in Vex

Recommended Posts

Hi,

In Python, we can create a "quick array" with conditions in a single line:

 

a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a2 = [0, 2, 4, 6, 8]
a3 = [i for i in a1 if i not in a2]


>>>> a3 == [1, 3, 5, 7, 9]

 

In vex, I know how to do it the long way:

 

int a1[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
int a2[] = [0, 2, 4, 6, 8];
int a3;
foreach( int i; a1 ){
	if( find(a2, i) >= 0 ){
    		append(a3, i);
    }
}


>>>> a3 == [1, 3, 5, 7, 9]

 

Is there a faster way to do this kind of things, maybe like in Python?

 

Thanks!

Edited by BenWall

Share this post


Link to post
Share on other sites

List comprehension?  Not really... Foreach is in itself shorthand,  for( int i = 0; i< len(a1); i++). 

You could create a custom VEX function library for stuff like this, close as you will get to python classes

Share this post


Link to post
Share on other sites

Too bad!

Anyway, I recently learned recently we can do this, which is easier to read to me:

if( find(a2, i) >= 0 ){
append(a3, i);
}



if( find(a2, i) >= 0 ) append(a3, i);

Thanks for your answer.

Edited by BenWall

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
Sign in to follow this  

×