cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Submit your abstract to the call for content for Discovery Summit Americas by April 23. Selected abstracts will be presented at Discovery Summit, Oct. 21- 24.
Choose Language Hide Translation Bar
Rotate a List

Problem

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.

Rotate a circular listRotate a circular list

Solution

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"};

Discussion

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.

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.