Choose Language Hide Translation Bar
Staff (Retired)

Rotate a List


You have a list of items that you want to rotate, moving items from the beginning of the list to the end or the end to the beginning. You might think of the list as circular.

animals.pngRotate a circular list


Use the Shift Into function, which does exactly that.

list = {"ape", "bat", "cow", "doe", "ewe", "fox", "gnu", "hog"};

Shift Into( list, 1 ); // move one from the front to the end
Show( list ); // {"bat", "cow", "doe", "ewe", "fox", "gnu", "hog", "ape"};

Shift Into( list, -2 ); // move two from the end to the front
Show( list ); // {"hog", "ape", "bat", "cow", "doe", "ewe", "fox", "gnu"};


There is also a Shift function that does not modify the source list but returns a new list.

list = Shift ( list, -2 ); // inefficient! don't do this!

The above example should use Shift Into rather than Shift to avoid making extra copies of a list. But if you need a new copy, modified, and need to keep the original, unmodified, use Shift.

See Also

Check out the scripting index (Help->Script Index) and take a look at the Insert Into and Remove From functions as well.

Article Labels
Article Tags