esProc Advanced Coding: Code Block

Advanced Code 545 0

In esProc Getting Started: Types of Cells, we learned that cells in esProc have many types, including constant cell, computational cell, executable cell and comment cell, and etc. The code blocks into which code is written in esProc also have the similar types: computational block, assignment block, executable block and comment block.

1. Computational block

In esProc advanced coding: Long Statements, we learned long statements and computational sub-statements. Instead of =, the cell strings in their master cells begin with ==, which tells esProc to regard the code block with this master cell as a single statement which cannot be separated and thus compute it as a whole. The program will ignore other cells after it executes the statement as required. For example:

  A B C
1 ==demo.query( “select NAME as CITY,  
2   STATEID as STATE from CITIES”)  
3 =demo.query( “select * from STATES where STATEID=?”,
4     STATE)
5   if left(B3.ABBR,1)==”N” >STATE=B3.NAME
6     =true
7   else =false

In the above code, the first two lines are a long statement with A1 being the master cell. It selects the desired cities information as follows:


The 3~7 lines are a computational sub-statement with A3 being the master cell. It selects from CITIES the cities that meets the criterion that the abbreviations of states to which they belong begin with “N” and assigns the names of the states as values to their STATE field. Result of A3 is as follows:


This block of statement beginning with “==” is also called as a computational block, which is actually a computational statement. A long statement or a computational sub-statement written in a single line can be also regarded as a computational block.

The master cell of a code block is its starting cell. Now let’s look at what a code block cell is.

The format of a code block should meet the following requirement: In the lines following the line where the master cell resides, the cells in the master cell column and those in the column to the left should be remain blank until the line where a cell in the master cell column or any cell to its left is non-blank. This means that, from this line on, cells no longer belong to the code block.

  A B C D
2   >Code begin    
6 Not null Not null    

In the above cellset, suppose B2 is the code block’s master cell. If the cells in the green area are blank, then we call the lines from the second to the fifth as the master cell’s code block. As neither A6 nor B6 in the red area is blank, regardless of the code cell, computational cell, executable cell, constant cell or comment cell, the code block ends from the sixth line.

A code block not only holds a single statement, it can also put a statement in its master cell in order to execute a loop or a branch, or other instructions. For example:

  A B C
1 =demo.query(“select NAME as CITY, STATEID as STATE from CITIES”) []  
2 for A1 =demo.query(“select * from STATES where STATEID=?”,A2.STATE)  
3   if left(B2.ABBR,1)==”N” >A2.STATE=B2.NAME
4     >B1=B1|A2

The above code uses code blocks instead of long statements or computational sub-statements. The code block with A2 being its master cell executes a loop and the one with B3 being the master cell executes a judgment instruction. B1 also gets the cities meeting the criterion that the abbreviations of states to which they belong begin with “N”.
However, the code blocks under discussion don’t include the case that statements are used to execute various instructions, but those cases where a statement is written in multiple cells according to sequential cells rule and thus should not be executed separately. The rule allows the code written in a computation cell or an execution cell to automatically move on to the next cell if it ends with characters, including “,”, “;” and “(“, until it doesn’t ends with these characters or reaches the end of the row (or the code block).

2. Assignment block and executable block

The double equal sign == at the head of the computational block has the similar function to that in a computational cell. If the expression in the code block doesn’t return a result, the code block will begin with double greater-than symbol >> and will still follow the sequential cells rule, meaning that the code block with the master cell is a single statement. For example:

  A B
1 ==demo.query(“select NAME as CITY, POPULATION from CITIES”)
2 >> left(CITY,1):”C”,
3   POPULATION>500000:true)

The long statement in A2 doesn’t return a result. It is used to select the data of cities whose names begin with “C” and whose population is greater than 500,000, and assign the result to C1. The computed result of C1 is as follows:


This code block for value assignment is called as an assignment block.

Some other long statements are used to execute certain instructions. For example:

  A B
1 =connect@e(“demo”) =A1.query(“select * from EMPLOYEE”)
2 >A1.execute(“drop table EMPLOYEE1”)  
3 >>A1.execute(“create table EMPLOYEE1( EID int,FULLNAME varchar(30),
4   GENDER varchar(10))”)
5 >>A1.execute(B1, “insert into EMPLOYEE1(EID,
6   FULLNAME,GENDER) values(?,?,?)”,
8 =A1.query(“select * from EMPLOYEE1”) >A1.close()

In this cellset, the code block in A3 is used to create a table – EMPLOYEE1– in the database. The code block in A5 is used to insert records into EMPLOYEE1 according to the data of B1. We can see the result of inserting records in A8 as follows:


The code blocks in A3 and A5 are called as execution blocks. The execution block and assignment block are defined according to their functions.

3. Comment block

Similarly, if the string in a cell starts with //, the code block with this cell being the master cell holds comments and it is thus called as a comment block. esProc will skip the whole comment block when encountering it. For example:

  A B
1 //comment 1.note…
2   2.note…
3   3.note…
4   =1+1
5 =1+1  

In the code block whose master cell is A1, all cells are naturally regarded as comment cells, without the need of being marked with a slash / in the front like the way a comment cell is handled. B4 is handled as a comment cell likewise. The non-blankness of A5 means the end of the code block, and the expression in A5 will be parsed and computed normally.

Different from a computational block, an assignment block and an executable block, there are no expressions in a comment block to be computed, so it doesn’t need the sequential cells rule.

Leave a Reply

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

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