Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data type in COBOL

Tags:

cobol

gnucobol

I have written the following program, I am confused why when I compile the program I get an error saying that A-COL(1,1) is not a numeric value while displaying A-COL(1,1) gives me 1.

   IDENTIFICATION DIVISION.
   PROGRAM-ID. TEST1.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 DIFF PIC 9(3).
       01 ARRAY.
           05 A-ROW OCCURS 99 TIMES.
               10 A-COL OCCURS 2 TIMES.
                   15 TABLE-CONTENT PIC 9(3).

   PROCEDURE DIVISION.
       MOVE 1 TO A-COL(1,1).
       MOVE 2 TO A-COL(2,1).
       DISPLAY A-COL(1,1).
       COMPUTE DIFF = A-COL(1,1) - A-COL(2,1).
       DISPLAY DIFF.
   STOP RUN.
like image 772
Louis Kuang Avatar asked Mar 14 '23 14:03

Louis Kuang


2 Answers

Change the A-COL definition to

"10 A-COL PIC 9(3) OCCURS 2 TIMES."

and remove the TABLE-CONTENT.

Group variables are considered alphanumeric (X type) so cannot be used in a computation.

Alternatively you may do this - refer to the address location using the actual numeric field.

PROCEDURE DIVISION.
   MOVE 1 TO TABLE-CONTENT(1,1).
   MOVE 2 TO TABLE-CONTENT(2,1).
   DISPLAY A-COL(1,1).
   COMPUTE DIFF = TABLE-CONTENT(1,1) - TABLE-CONTENT(2,1).
   DISPLAY DIFF.

Also you might want to make DIFF signed.

Additional Information

A-COL(1,1) displays "1" because it stores the data as "1xx" where x = space. That is not a numeric value. When you run the solutions here you will notice that display line produces "001".

like image 148
uncaught_exception Avatar answered May 16 '23 04:05

uncaught_exception


Keep your WORKING-STORAGE structure the same. However, your data-elements are not A-COL, but THE-CONTENT. So use THE-CONTENT, not A-COL.

   IDENTIFICATION DIVISION.
   PROGRAM-ID. TEST1.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01  DIFF PIC S9(3).
   01  ARRAY.
       05  A-ROW 
           OCCURS 99 TIMES.
           10  A-COL 
               OCCURS 2 TIMES.
               15 TABLE-CONTENT            PIC 9(3).

   PROCEDURE DIVISION.
       MOVE 1                       TO TABLE-CONTENT ( 1 1 )
       MOVE 2                       TO TABLE-CONTENT ( 2 1 )
       DISPLAY 
               ">"
               TABLE-CONTENT ( 1 1 )
               "<"
       COMPUTE DIFF                 = TABLE-CONTENT ( 1 1 ) 
                                    - TABLE-CONTENT ( 2 1 )
       DISPLAY 
               ">"
               DIFF
               "<"
       STOP RUN
       .

Your structure is better, because it is easier to maintain. If you ever want to REDEFINES TABLE-CONTENT, you can, without changing the structure. Not so if you "complicate" the structure now.

Yes, if you MOVE a numeric literal to a group-item, an alpha-numeric MOVE is carried out, the result will be your literal left-justified and space-padded to the right, or truncated to the right, or just fitting, depending on the size of your literal.

Although conceptually you have "columns" in your table (COBOL doesn't have arrays, it has tables with OCCURS), be aware that you cannot access a column as a whole. In storage you have row-1-col-1, row-1-col-2, row-2-col-1, row-2-col-2 through to row-99-col-1, row-99-col-2.

Any arithmetic (ADD, SUBTRACT, MULTIPLY, DIVIDE and COMPUTE) can only use numeric fields or literals as source-data. It is not enough that a field contains "a number", it must be a numeric field.

The GIVING (of ADD, SUBTRACT, MULTIPLY and DIVIDE) can place the result in a non-numeric field of a particular type, a numeric-edited field. This is a field with a PICture clause containing numeric-editing PICture symbols.

like image 41
Bill Woodger Avatar answered May 16 '23 06:05

Bill Woodger