sadboy Posted February 18, 2021 Share Posted February 18, 2021 (edited) 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 February 18, 2021 by sadboy forgot to add a line of code Quote Link to comment Share on other sites More sharing options...
anim Posted February 18, 2021 Share Posted February 18, 2021 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} 1 Quote Link to comment Share on other sites More sharing options...
snoot Posted February 18, 2021 Share Posted February 18, 2021 (edited) 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 February 18, 2021 by snoot 1 Quote Link to comment Share on other sites More sharing options...
sadboy Posted February 18, 2021 Author Share Posted February 18, 2021 thank you! It was very helpful! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.