我仅尝试打印此COBOL表的元素之一,但是似乎在理解COBOL中的数组(表)如何工作时遇到了麻烦!
data division.
working-storage section.
01 m-airlines.
05 m-united occurs 3 times.
10 m-u-a330 pic 9(1) value 3.
10 m-u-a321 pic 9(1) value 1.
10 m-u-a300 pic 9(1) value 5.
05 m-delta occurs 3 times.
10 m-dl-a330 pic 9(2) value 35.
10 m-dl-a321 pic 9(2) value 27.
10 m-dl-b777 pic 9(2) value 20.
procedure division.
display "number of Delta's Airbus a330: " m-delta(1,1).
我收到此错误:
下标过多。
答案 0 :(得分:1)
根据代码,您有两个表元素(或)2个一维表m-united
和m-delta
。要引用表中的项目,应使用表元素的数据名称,并在括号中使用它的出现编号(下标)。这称为下标。正如@Bruce Martin在评论中提到的那样,您应该尝试m-delta(1)
引用表中的第一个元素。
您引用表格元素m-delta(1,1)
的方式适用于二维表格。这种引用方式是错误的,因为您尚未定义2D表。而是定义了2个一维表。有关创建2D / 3D表的更多信息,请参见Nesting tables。
答案 1 :(得分:1)
这些是您的代码生成的表。这不是您的意图。
+-------------+-------------+-------------+
m-united(1): | m-u-a330(1) | m-u-a321(1) | m-u-a300(1) |
+-------------+-------------+-------------+
m-united(2): | m-u-a330(2) | m-u-a321(2) | m-u-a300(2) |
+-------------+-------------+-------------+
m-united(3): | m-u-a330(3) | m-u-a321(3) | m-u-a300(3) |
+-------------+-------------+-------------+
+--------------+--------------+--------------+
m-delta(1): | m-dl-a330(1) | m-dl-a321(1) | m-dl-b777(1) |
+--------------+--------------+--------------+
m-delta(2): | m-dl-a330(2) | m-dl-a321(2) | m-dl-b777(2) |
+--------------+--------------+--------------+
m-delta(3): | m-dl-a330(3) | m-dl-a321(3) | m-dl-b777(3) |
+--------------+--------------+--------------+
也许这就是您想要的:
01 AIRLINE-TABLE.
05 M-U-A330 PIC 99 VALUE 3.
05 M-U-A321 PIC 99 VALUE 1.
05 M-U-A300 PIC 99 VALUE 5.
05 M-U-B777 PIC 99 VALUE 0.
05 M-D-A330 PIC 99 VALUE 35.
05 M-D-A321 PIC 99 VALUE 27
05 M-D-A300 PIC 99 VALUE 0.
05 M-D-B777 PIC 99 VALUE 20.
01 FILLER REDEFINES AIRLINE-TABLE.
05 AIRLINE OCCURS 2 TIMES.
10 NUM-A330 PIC 99.
10 NUM-A321 PIC 99.
10 NUM-A300 PIC 99.
10 NUM-B777 PIC 99.
它将生成:
+-------------+-------------+-------------+-------------+
airline(1): | num-a330(1) | num-a321(1) | num-a300(1) | num-b777(1) |
+-------------+-------------+-------------+-------------+
airline(2): | num-a330(2) | num-a321(2) | num-a300(2) | num-b777(2) |
+-------------+-------------+-------------+-------------+
... DISPLAY "Number of Delta's Airbus a330 : " num-a330(2)
或者这个:
01 AIRLINE-TABLE.
05 M-U-A330 PIC 99 VALUE 3.
05 M-U-A321 PIC 99 VALUE 1.
05 M-U-A300 PIC 99 VALUE 5.
05 M-U-B777 PIC 99 VALUE 0.
05 M-D-A330 PIC 99 VALUE 35.
05 M-D-A321 PIC 99 VALUE 27
05 M-D-A300 PIC 99 VALUE 0.
05 M-D-B777 PIC 99 VALUE 20.
01 FILLER REDEFINES AIRLINE-TABLE.
05 AIRLINE OCCURS 2 TIMES.
10 AIRPLANE OCCURS 4 TIMES.
15 NUM-PLANES PIC 99.
给出
+------------------+------------------+------------------+------------------+
airline(1): | num-planes(1, 1) | num-planes(1, 2) | num-planes(1, 3) | num-planes(1, 4) |
+------------------+------------------+------------------+------------------+
airline(2): | num-planes(2, 1) | num-planes(2, 2) | num-planes(2, 3) | num-planes(2, 4) |
+------------------+------------------+------------------+------------------+
... DISPLAY "Number of Delta's Airbus a330 : " num-planes(2, 1)