Issue while populating array in COBOL
I am getting very strange scenario. I have one array defined in my COBOL pgm.
05 A-TABLE.
10 A-TABLE-LIST OCCURS 10 TIMES INDEXED BY A-IDX.
15 FILLER PIC X(7) VALUE '<TEST>'.
15 A-LIST-VALUE PIC X(30).
开发者_StackOverflow社区 15 FILLER PIC X(8) VALUE '</TEST>'.
I am setting A-IDX=1 and moving 'XYZ' to A-LIST-VALUE(A-IDX). While displaying A-TABLE, it is showing as XYZ------------------------------ and all spaces... :( I am not getting what is the issue here? Can anyone help me to resolve this?
Regards, Saisha.
You cannot set values for a table that way. One way to set values in a table is to use a REDEFINES and a separate data area.
05 A-TABLE-X.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
10 FILLER PIC X(45)
VALUE '<TEST> </TEST>'.
05 A-TABLE REDEFINES A-TABLE-X.
10 A-TABLE-LIST OCCURS 10 TIMES INDEXED BY A-IDX.
15 FILLER PIC X(7).
15 A-LIST-VALUE PIC X(30).
15 FILLER PIC X(8).
That is pretty cumbersome. Another method is to MOVE the data in at runtime in an initialisation paragraph.
05 A-TABLE REDEFINES A-TABLE-X.
10 A-TABLE-LIST OCCURS 10 TIMES INDEXED BY A-IDX.
15 A-LIST-A PIC X(7).
15 A-LIST-VALUE PIC X(30).
15 A-LIST-B PIC X(8).
PERFORM VARYING A-IDX FROM 1 BY 1 UNTIL A-IDX > 1
MOVE '<TEST> TO A-LIST-A(A-IDX)
MOVE SPACES TO A-LIST-VALUE(A-IDX)
MOVE '</TEST> TO A-LIST-B(A-IDX)
END-PERFORM
I didn't try compiling any of these, this is just freehand.
As a side note, if you are using Enterprise COBOL version 3.2 or higher and you are trying to create XML in COBOL, there exists an XML GENERATE statement.
To my understanding, your question is why "XYZ<27 spaces>" is being displayed when only "XYZ" was moved. If so, you need to initialize
before moving and trim
the spaces before displaying or moving into another variable.
If your problem is not yet solved, describe much otherwise let us know how it was resolved.
精彩评论