RevSequence
This function returns an array containing values selected from a given series of integers, either in the original order or in reverse order.
Syntax
RevSequence(<start>, <end> [, <step> [, <count>[, <policy>[, <seed>]]]])
Parameters
<start>
Type: Long
The first possible value.
<end>
The last possible value.
Type: Long
<step>
(Optional.) The increment between consecutive possible values. The default value is 1.
Type: Long
<count>
Type: Long
(Optional.) The number of items to return. If omitted, negative, or greater than the number of possible values, all items are returned.
<policy>
(Optional.) Controls the way the list order is chosen. See
Possible values for Policy. The default value is 1.
Long
<seed>
Type: Long
(Optional.) The state to be used for the reversal. The default value is 0.
(return)
Type: None
An array containing a selection from the set of possible numbers.
Notes
The RevSequence function returns an array containing numbers from the sequence <start>, <start>+ <step>, <start> + <step> * 2, ... , <start> + <step> * N (where N is the maximum integer such that the sequence doesn’t pass End), either in the normal order or in reverse order. <step> can be positive or negative. If <step> is 0, Count copies of <start> are returned, unless Count is omitted or negative, in which case an empty array is returned. If <step> is positive and <start> > <end>, or if <step> is negative and <start> < <end>, an empty array is returned.
The <start>, <end>, and <step> arguments define the potential set of numbers to return (for example, a setting of (1, 10, 2) produces the set {1, 3, 5, 7, 9}, while (10, 1, -2) produces {10, 8, 6, 4, 2}). Then <policy> and <seed> and the reversal state determine in which order those numbers are returned. <count> controls how much of that ordered set is returned.
This function uses the reversal state stored by the mrScriptBasic engine or Evaluate component and passed as a hidden parameter. However, when both <policy> and <seed>are nonzero, the reversal state is set to <seed> before the <policy> is applied. If the resulting reversal state is even, the list is presented in normal order; if odd, the list is reversed.
Example
This example uses RevSequence in an SQL query. It uses RevSequence four times to select five values from the values 1 to 10, each time using a different value for the <policy> parameter, so that sometimes the values are presented in the normal (specified) order and sometimes in reversed order.
SELECT
RevSequence(1, 10, 1, 5, 0) AS Policy0,
RevSequence(1, 10, 1, 5, 1) AS Policy1,
RevSequence(1, 10, 1, 5, 2) AS Policy2,
RevSequence(1, 10, 1, 5, 3) AS Policy3
FROM vdata
Here are the results for the first five respondents:
Policy0 Policy1 Policy2 Policy3
----------- ----------- ------------ ------------
1 {1,2,3,4,5} {1,2,3,4,5} {10,9,8,7,6} {1,2,3,4,5}
2 {1,2,3,4,5} {1,2,3,4,5} {1,2,3,4,5} {1,2,3,4,5}
3 {1,2,3,4,5} {1,2,3,4,5} {10,9,8,7,6} {10,9,8,7,6}
4 {1,2,3,4,5} {1,2,3,4,5} {1,2,3,4,5} {10,9,8,7,6}
5 {1,2,3,4,5} {1,2,3,4,5} {10,9,8,7,6} {1,2,3,4,5}
The function orders the list of values before selecting the required number of values.
▪In the first column, Policy0, the list is in the normal order because <policy> is set to 0. This means that the list is always presented in the normal order.
▪In the second column, Policy1, the list is also in the normal order. This time <policy> is set to 1, which means that whether the list is reversed or not is determined by the current setting of the reversal state. Since the <seed> parameter is not specified, the reversal state remains at its initial value of 0, which is an even number, and so the list is always presented in the normal order. If you change the query to specify the <seed> parameter as 1, the order is always reversed, because 1 is an odd number.
▪In the third column, Policy2, the list is in normal and reverse order alternately. This is because <policy> is set to 2, which means that each time the function is called, one is added to the reversal state, thus causing it to alternate between odd and even values.
▪In the fourth column, Policy3, the list is in normal and reverse order at random because <policy> is set to 3. This means that the reversal state is set to a random value each time the function is called, thus causing it to switch randomly between odd and even values.
See also