SHIFT numberOfPlaces [fillNumber] inputVector resultVariable

Shifts the elements of inputVector the given number of places to the right or left and puts the result in resultVariable. If numberOfPlaces is positive, the shift will be to the right. If it is negative, the shift will be to the left. If numberOfPlaces is zero, the vector will not be shifted.

The size of the resultVariable will be the same as that of the inputVector, therefore numberOfPlaces elements will be eliminated from the vector, that is, they will be "shifted out" of the vector.

If fillNumber is present, its first element will be repeatedly shifted in to fill the shifted elements' original positions. If fillNumber is absent, zeros will be shifted in.

InputVector itself is left unchanged.

See also: ROTATE

The following program illustrates the operation of the SHIFT command.

COPY 1,10 v
SHIFT  4 v vRight4
SHIFT -4 v vLeft4
PRINT v vRight4 vLeft4

Here is the output of the above program:

v: (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)
vRight4: (0.0 0.0 0.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0)
vLeft4: (5.0 6.0 7.0 8.0 9.0 10.0 0.0 0.0 0.0 0.0)

If you want a right shift to fill in by repeating the first element (assuming you don't know what that first element is so you have to get it from the vector), you can reuse the input vector as the fillNumber since fillNumber will only use the first element of its vector. For example:

COPY 1,10 vec
SHIFT 5 vec vec shiftedVec1  'shift right, fill in with 1st element
SHIFT -5 vec vec shiftedVec2 'shift left, fill in with 1st element
PRINT vec shiftedVec

Which produces this output:

vec: (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)
shiftedVec1: (1.0 1.0 1.0 1.0 1.0 1.0 2.0 3.0 4.0 5.0)
shiftedVec2: (6.0 7.0 8.0 9.0 10.0 1.0 1.0 1.0 1.0 1.0)

Assuming that you don't know the final element of the vector being shifted but you want to repeat its last element on a left shift, you'll have to work a little harder:

COPY 1,10 vec
SIZE vec vecSize  'Find size so can use it to get last element
TAKE vec vecSize fillNumber 'Get last element into fillNumber
SHIFT -5 fillNumber vec shiftedVec
PRINT vec shiftedVec

This produces the following output:

vec: (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)
shiftedVec: (6.0 7.0 8.0 9.0 10.0 10.0 10.0 10.0 10.0 10.0)