esProc Program: Sequence

esProc 327 0

A sequence is an ordered set consisting of some data, which are called members of the sequence. A sequence is similar to an array in high-level languages, but the data types of its members not necessarily the same. The following will explain its basic computation through creation, access, operators and functions.


1.1 Create with Constants

Bracketing one or more constants with “[]” represents a sequence constant. Bracketing the members in an expression with “[]” makes a sequence. e.g.

  A B C
1 1 red 2013-06-04
2 2 blue 27.49
3 3 yellow Tom
4 [15.2,b,1] =[A1:C3] =[3,A4,B4]
5 [1,2,3,3] [] [[]]

In the cellset, A4, A5, B5 and C5 contain respectively a sequence constant. B4 and C4 contain sequences obtained by computing the expressions.

Members of the sequence in A4 are of various data types, including float, string and integer. Members in B4 are from cell values. In C4, the members even include a sequence. And there are identical members in the sequence in A5. In turn, the data in A4, B4, C4 and C5 are as follows:


Let’s compare the two values in B5 and C5:


It can be seen that the value in B5 is an empty sequence, while the value in C5 is a non-empty one, whose only member is an empty sequence.

Note: Members of a sequence can be of any data types, including basic types, sequences, records, etc. A sequence that all its members are integers is called an integer sequence.

1.2 Create with functions

1 =to(2,6)
2 =”1,a,b,c”.array()
3 =periods@y(“2012-08-10”,date(now()),1)
4 =file(“sales.txt”).import@t()

The code in A1 represents a sequence composed of the consecutive integers from 2 to 6; the code for the sequence can be abbreviated as to(6) if the sequence begins at 1. The code in A2 splits a string into a sequence. And the code in A3 creates a date sequence representing a time period whose starting and ending point are in two adjacent years respectively. The results of A1, A2 and A3 are as follows:


A4 imports records from a structured text file and creates a sequence. The values of the sequence are as follows:


A sequence whose members are records is a table sequence, which is often used to perform structured data computing. It’s not the focus of this article, for more information please refer to esProc Program: Table Sequence Operation.

1.3Create by computing

The code in the following cellset is for importing the text file sales.txt into the table sequence A1. Fetch STATE column to create sequence A2; group the records according to STATE to create sequence A3:

1 =file(“sales.txt”).import@t()
2 =A1.(STATE)

After computation is finished, the sequence in A2 is as follows:


The sequence in A3 is as follows:


It can be seen that members of the sequence in A3 are sequences, whose members are records.


2.1 Access members according to sequence numbers

  A B
1 [a,b,c,d,e,f,g]  
2 =A1(2) =A1.m(2)
3 =A1([2,3,4]) =A1(to(2,4))
4 =A1.m(-1)  

The expressions in A2 and B2 are equal. Both of them fetch the second member from the sequence. The value is as follows:


A3 fetches members of 2 ~4 from the sequence, with the value being a sequence. Please note [2,3,4] is also a sequence (integer sequence). The results of the expressions in B3 and A3 are the same:


A4 fetches the last member of the sequence. Please note A.m() function must be used when members are fetched backwards and the expression cannot be abbreviated to A1(-1). The result is as follows:


2.2 Assignment and modification

1 [a,b,c,d,e,f,g]
2 >A1(2)=”r”
3 >A1([3,4])=[“r”,”s”]
4 >A1.modify(4,[ “r”,”s”])

In order to specify the change of the statement in e ach cell, click  in the tool bar to execute the code step by step.

A2 modifies the second member into r; A3 goes on to modify the third and fourth members in A1. A4 continues the modification in order from the fourth member. The expression in A4 equals >A1([4,5])=[“r”,”s”]. With the step-by-step execution, the changes of sequence in A1 are shown as follows:


2.3 Add members

1 [a,b,c,d,e,f,g]
2 >A1.insert(0,”r”)
3 >A1.insert(2,[“r”,”s”,”t”])

A2 inserts a new member at the end of the sequence. A3 continues to insert three members consecutively before the second member. When executed step by step, the sequence in A1 has undergone the following changes:


2.4 Delete members

1 [a,b,c,d,e,f,g]
2 > A1.delete(2)
  > A1.delete([2,4])

A2 deletes the second member. A3 continues to delete the second and the fourth members. When executed step by step, the sequence in A1 has undergone the following changes:


3. Operators

3.1 Sets computation

Sets operations include ^ (intersection), & (union), \ (difference), and | (concatenation), etc. For example:

  A B
1 [a,b,1,2,3,4] [d,b,10,12,3,4]
2 =A1^B1 =A1\B1
3 =A1&B1 =A1|B1

The sequences in A1 and B1 are as follows:


A2, B2, A3 and B3 compute respectively the intersection, difference, union and concatenation of the two sequences. After the computation is finished, the results of A2, B2, A3 and B3 are as follows:


Note: Both union and concatenation are created by combining members of two sequences in order. Their common members only appear once in union while, in concatenation, all the duplicates will appear.

3.2 Alignment arithmetic operations

Two sequences of the same length can perform alignment operation according to members and return a sequence. The operation includes ++ (addition), — (subtraction), ** (multiplication), // (division) and %% (remainder). For example: 

  A B
1 [1,2,3,4] [10,12,3,4]
2 =A1++B1 =A1–B1
3 =A1**B1 =A1//B1

The sequences in A1 and B1 are as follows:


A2, B2, A3 and B3 perform addition, subtraction, multiplication and division respectively on the two sequences in alignment. After computing, the results of A2, B2, A3 and B3 are as follows:


3.3 Boolean operation

Two sequences can be compared in alignment. The result is of Boolean type. For example:

1 =[1,2,3]==[1,2,3]
2 =[1,”B”,3]<=[1,”b”,4]
3 =[1,2,3]<[1,3,4]

The result in A1 is true, meaning the two sequences are equal. The result in A2 is true, because B is less than b. The result in A3 is true because the second member of the first sequence -2- is less than the one -3- in the second sequence:


Note: function is used to judge the inclusion relation between sequences.

4. Functions

4.1 aggregate function

aggregate functions include A.sum(),A.avg(), A.max() and A.variance() etc., respectively for computing sum, average, maximum value and variance. The following is an example:

1 [2,4,6]
2 =A1.sum()
3 =A1.sum(~*~)

A2 computes the sum of the sequence’s members. A3 computes the sum of every member’s square. Results of A2 and A3 are as follows:


4.2   loop functions

loop functions can perform computation on each member of a sequence. Simple as they are, the loop functions can be used to simplify the complex loop statements, which include loop operation, filter, locate, query, rank and sort, etc. For example:

  A B C
1 [2,4,-6] =A1.(~+1)  
2>1) =A1.pselect@a(~>1)  
3 =A1.rank() =A1.sort() =A1.sort(~:-1)

A2 adds 1 to each member of the sequence. The results are as follows:


A2 selects members that are greater than 1. B2 selects the sequence numbers of members that are greater than 1. C2 queries the sequence numbers of member -6 and member 2 in sequence A1. The results of A2, B2 and C2 are as follows:


A3 computes the rankings of each member in the sequence. B3 sorts members of the sequence in ascending order, and C3 does it in descending order. The results of A3, B3 and C3 are as follows:


Leave a Reply

Hi,You need to fill in the Username and Email!

  • Username (*)
  • Email (*)
  • Website