Scripting > UNICOM Intelligence Function Library > List functions > RevSequence
 
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 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 is 1.
Long
Seed
Type: Long
Optional. The state to be used for the reversal. The default is 0.
(return)
Type: None
An array containing a selection from the set of possible numbers.
Remarks
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 may 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.
Note that 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 the Policy and Seed parameters and the reversal state determine in which order those numbers are returned. Finally, the Count argument 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 shows using RevSequence in an SQL query. The example uses RevSequence four times to select five values from the values 1 through 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 we changed the query to specify the Seed parameter as 1, the order would always be 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
Rev
List functions