Language and Conventions Overview | Arrays
它经常需要引用数组,而不是整个数组部分。这可以通过使用子区域操作者来实现“..”。例如,如果变量A为15的一维阵列,然后通过12号元素的第七可以用A(7..12)被引用。
子范围操作者可以使用具有任何数据类型。子范围的第一个值表示的下限和第二值表示要处理的范围内的上限。仅一个子区域操作者可以在一个单一的参考可以使用,因此,在多维数组中,只有一个在尺寸可以在一个单一的参考加以解决。
The following example shows the use of both one and two dimensional arrays. The range of the array is defined at the time it is declared.
GRIPSW/ DECLRV
NUMBER/ NUM1X5(5), NUM4X4(4,4), TMPCNT
ENTITY/ ENT1X5(5), ENT4X4(4,4)
STRING/ STR1X5(5,132), STR4X4(4,4,132)
DATA/NUM4X4, 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42,$
43, 44
NUM1X5(1..3) = NUM4X4(2,1..3)
ENT1X5(1) = POINT / NUM1X5(1..3)
ENT1X5(2) = POINT / NUM4X4(4,2..4)
TMPCNT = 2
BLANK / ENT1X5(1..TMPCNT)
STR1X5(1) = 'HELLO'
STR1X5(2) = 'WORLD'
NOTE / 1,1, STR1X5(1..2)
HALT
The lines are created between a range of points. The points are then deleted by referring to the array variable.
There are three methods of expressing the upper and lower limits of a subrange operator. These methods are the constant array subrange, the fixed array subrange, and the variable array subrange.
A constant array subrange has the upper and lower limits as constants or arithmetic expressions that do not include variables.
For example:
N (1..3)
N (3*2..10)
N (2*2..3+4)
In a fixed array subrange, the lower limit is a simple variable and the upper limit includes the same variable added to a constant or an arithmetic expression.
For example:
N(I..I+3)
N(K,L..L+5)
N(I,J,K..K+(5*2))
With a variable array subrange, the upper and lower limits may be expressed in the form of constants, variables, and arithmetic expressions.
For example:
N(I..I*2)
N(ABSF(X+3).. (J-2)**K)
N(I..J)
Properly used, constant and fixed array subranges may be used anywhere in a GRIP program where subscripted arrays are applicable. Variable array subranges, however, are limited to functions which have number, object, or string lists and some functions which use arrays.
Assume that the following arrays have been defined:
ENTITY/E1(50),E2(10,10)
NUMBER/N1(200),N2(10,10),N3(5,10)
The following table shows some uses of subrange operators.
Statement |
Comment |
N3 = N1(I..J) |
Valid. No compilation error. At run time, error checks will be made on array bounds and the subrange. |
N1(6..10) = N1(1..5) |
Valid assignment. |
POINT/N1(20..22) |
Valid. |
POINT/N1(I..I+2) |
Valid. |
POINT/N1(J..K) |
Invalid to use variable array subrange in a POINT definition. |
HATCH/E1(J..K) |
Valid. Variable subrange can be used with HATCH statement. |
GROUP/E2(1..I,1..J) |
Invalid. Only one array subrange can be used in a multidimensional array. |
When some statements use a subrange of objects, they will generate a run time error if each array position is not assigned an object. This can happen when the objects are loaded into the array during program execution. The following example shows the use of a subrange operator with the IDENT statement to prompt the user to select a number of points to create a spline. These points are assigned to the OBJ array.
ENTITY/OBJ(25),SP1
L10:IDENT/'PICK SPLINE
POINTS',OBJ,CNT,NUM,RESP
JUMP/L10:,TERM:,,,,RESP
SP1=SPLINE/ENT(1..NUM)
The SPLINE statement will generate a run time error if all array positions specified are not assigned points during user selection (IDENT).
You can overcome this situation by using the minor word CNT in the IDENT statement to cause the system to count the number of objects selected and assign them to a numerical variable (NUM). You then use the variable as the subrange operator in the SPLINE statement.
Therefore, if the user selects 14 points, the SPLINE statement SP1=SPLINE/OBJ(1..NUM) translates to SP1=SPLINE/OBJ(1..14). This causes only those array positions which were assigned objects to be addressed.
For a subrange of objects, there is a 1000 object limit per subrange. To specify more than 1000 objects, use the following syntax (this example specifies 2050 objects):
E(2050)
E(1..1000),E(1001..2000),E(2001..2050)