Jump to content
sadboy

Shift Array Vex

Recommended Posts

How can I shift values in an array using a for loop?
For example: [0,0,0,1,1,1]->[1,0,0,0,1,1]

I would think that this code would work but it does not. Any help would be appreciated.

for(int j = 0; j < 6; j++ ){
    if(@new_tile_array[@new_prim_id] == 1){
        break;
    } else{
        int popped = pop(@new_tile_array);
        append(@new_tile_array,popped);
    }
}

 

Edited by sadboy
forgot to add a line of code

Share this post


Link to post
Share on other sites
int a[] = array(0,0,0,1,1,1); // original array
int last = pop(a); // pop last element
insert(a, 0, last); // insert it to 0th index aka prepend
// a now contains {1,0,0,0,1,1}

 

  • Like 1

Share this post


Link to post
Share on other sites

Another option:

If you need to create wrapping behaviour and want to avoid a loop, with modulus you can create "wrapping" behaviour.

Essentially split the  array in two and swap the start and end. What defines the offset is where you put the split.

int a[] = array(0,1,2,3,4,5,6);
int i = len(a);

// offset value
int offset = 1;

int out[] = a[offset%i:i+1];
append(out,a[0:offset%i]);

i[]@out = out;

 

Edited by snoot
  • Like 1

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

×