Jump to content

Shift Array Vex


sadboy

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