Problem:
There is some confusion about how the VALUE NEXT phrase works when it is defined for a 78 level constant data-item that is embedded within a group item containing the REDEFINES clause.
Using the example below, the following questions are asked:
Is there any reason why the value of MTCount is 23 and not 12?
Is there any reason why uncommmenting TABLE-TWO will correct the problem?
01 TABLE-ONE.
78 LTSize VALUE 17.
78 LTStart VALUE NEXT.
05 WS-LEVEL-TABLE.
10 FILLER PIC X(17) VALUE "Doctor".
10 FILLER PIC X(17) VALUE "CRNA".
10 FILLER PIC X(17) VALUE "OR Surgeon".
10 FILLER PIC X(17) VALUE "Patient Type".
10 FILLER PIC X(17) VALUE "Location".
10 FILLER PIC X(17) VALUE "Procedure".
10 FILLER PIC X(17) VALUE "Modifier".
78 LTw VALUE NEXT.
78 LTCount VALUE NEXT - LTStart / LTSize.
05 WS-LEVEL-TABLE-REDEF REDEFINES WS-LEVEL-TABLE.
10 WS-LEVEL PIC X(17) OCCURS LTCount.
*01 TABLE-TWO.
78 MTSize VALUE 9.
78 MTStart VALUE NEXT.
05 WS-MONTH-TABLE.
10 FILLER PIC X(9) VALUE "January ".
10 FILLER PIC X(9) VALUE "February ".
10 FILLER PIC X(9) VALUE "March ".
10 FILLER PIC X(9) VALUE "April ".
10 FILLER PIC X(9) VALUE "May ".
10 FILLER PIC X(9) VALUE "June ".
10 FILLER PIC X(9) VALUE "July ".
10 FILLER PIC X(9) VALUE "August ".
10 FILLER PIC X(9) VALUE "September".
10 FILLER PIC X(9) VALUE "October ".
10 FILLER PIC X(9) VALUE "November ".
10 FILLER PIC X(9) VALUE "December ".
78 MTw VALUE NEXT.
78 MTCount VALUE NEXT - MTStart / MTSize.
05 WS-MONTH-REDEF REDEFINES WS-MONTH-TABLE.
10 WS-S-MTH PIC X(9) OCCURS MTCount.
Resolution:
Answers to the questions:
There is not only a reason but there is also an explanation and an example within the Net Express documentation.
The explanation is that when specifying the VALUE NEXT phrase of a constant data item, it is actually referring to the next byte of storage which will occur directly after the definition of the "PRECEDING" data-item. It does not automatically point to the offset of the next data item which is being defined. (although this is how most people do interpret this)
In the example:
01 TABLE-ONE.
78 LTSize VALUE 17.
78 LTStart VALUE NEXT.
05 WS-LEVEL-TABLE.
10 FILLER PIC X(17) VALUE "Doctor".
10 FILLER PIC X(17) VALUE "CRNA".
10 FILLER PIC X(17) VALUE "OR Surgeon".
10 FILLER PIC X(17) VALUE "Patient Type".
10 FILLER PIC X(17) VALUE "Location".
10 FILLER PIC X(17) VALUE "Procedure".
10 FILLER PIC X(17) VALUE "Modifier".
78 LTw VALUE NEXT.
78 LTCount VALUE NEXT - LTStart / LTSize.
05 WS-LEVEL-TABLE-REDEF REDEFINES WS-LEVEL-TABLE.
10 WS-LEVEL PIC X(17) OCCURS LTCount.
*01 TABLE-TWO.
78 MTSize VALUE 9.
78 MTStart VALUE NEXT.
05 WS-MONTH-TABLE.
10 FILLER PIC X(9) VALUE "January ".
10 FILLER PIC X(9) VALUE "February ".
10 FILLER PIC X(9) VALUE "March ".
10 FILLER PIC X(9) VALUE "April ".
10 FILLER PIC X(9) VALUE "May ".
10 FILLER PIC X(9) VALUE "June ".
10 FILLER PIC X(9) VALUE "July ".
10 FILLER PIC X(9) VALUE "August ".
10 FILLER PIC X(9) VALUE "September".
10 FILLER PIC X(9) VALUE "October ".
10 FILLER PIC X(9) VALUE "November ".
10 FILLER PIC X(9) VALUE "December ".
78 MTw VALUE NEXT.
78 MTCount VALUE NEXT - MTStart / MTSize.
05 WS-MONTH-REDEF REDEFINES WS-MONTH-TABLE.
10 WS-S-MTH PIC X(9) OCCURS MTCount.
When commenting out the 01 level of TABLE-TWO then the preceding data item will actually be changed that will be used to calculate the next byte of storage.
In the Animator, look at the values of the followiing two items:
1.
LTStart = 352.
2.
MTStart = 369.
Subtracting LTStart from MTStart will result in the value 17.
This is because MTStart is referring to the next byte of storage after the definition of the data item:
10
WS-LEVEL PIC X(17).
So the value of MTStart is actually pointing to the second occurrence of the data item WS-LEVEL within the redefined table.
By uncommenting the 01 TABLE-TWO line then the value of MTStart will then be 472 because the preceding data item is now TABLE-TWO instead of WS-LEVEL.
And everything will work as expected.
The explanation and example can be found in the Net Express 4.0 Help->Contents->Reference->COBOL Language->Part 2:Program Definition->Examples->Next phrase of constant names.
The rules for this can also be found under Help->Contents->Reference->COBOL Language->Part 2:Program Definition->Data Division->File and Data Description->The Value clause.
If trying to calculate the number of occurrences within the table the VALUE START or LENGTH phrases can be used, which are also documented under the reference above.
