rotate left an array

Fausto fausap at outlook.it
Mon Oct 3 18:09:05 UTC 2022


Hello all,

I am trying to rotate left an array.
I found a very basic way, and I am not sure if there is something 
clever than this :) maybe using slices...
the external for represents how many times you are rotating (in 
this case 2).

```d
void main()
{
     import std.range;
     import std.stdio: write, writeln, writef, writefln;

     int[] arr1 = [ 1, 2, 3, 4 ];
     int t = 0;

     writeln(arr1);

     for (int j = 0;j<2;j++) {
     t = arr1[0];
     for(int i = 0;i<arr1.length-1;i++) {
         arr1[i] =  arr1[i+1];
     }
     arr1[$-1] = t;
     }

     writeln(arr1);

}
```

thanks a lot
Fausto


More information about the Digitalmars-d-learn mailing list