1

New to COBOL.. I would like to populate a table. I want it to have two columns and eight rows in each column. I want to "hard code" these different values into the first colum, with one in each field. They are Non-integer fixed point values (Swedish comma notation): 16,0 17,0 18,5 25,0 30,0 35,0 40,0 99,0. The second column should be filled with strings up to 30 charachters in each row/field. I think I've managed to do the table, however, I don't know how to populate it in the best way.

           01 BMI_tabell. 
                05 tabell_columns_highbreak OCCURS 2 TIMES. 
                    10 Cutoffhigh_rows OCCURS 8 TIMES.
                        15 numBMI_cat_high_break PIC 99.9. 
                    
                    10 BMI_meddelande_kort_rows OCCURS 8 TIMES.
                        15 BMI_meddelande_kort_cat_X pic X(30).

This is in the "data division." under "working-storage section."

I tried initializing each table item individually, according to IBM documentation, but did not get it to work due to the "nested" nature of the table.

4 Answers4

1

You're over defining your table.

Here's a different definition.

            01 BMI-tabell. 
               05 FILLER             OCCURS 8 TIMES.
                  10 numBMI-cat-high-break     PIC 99V9. 
                  10 BMI-meddelande-kort-cat-X pic X(30).

The two columns are the two 10 values. The 8 rows are defined by the FILLER value.

I used an internal PICTURE for the BMI number. You use a 99.9 PICTURE when you want to output the number. The 99V9 PICTURE allows you to use the number for calculations and comparisons.

I changed all your underscores to dashes because that's what COBOL programmers expect to see when reading your code.

Hint: Turn caps lock on when typing COBOL code.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

To access multi-dimensional tables in PROCEDURE DIVISION - just separate it by comma:

                MOVE 16.0 TO numBMI_cat_high_break (1, 1)
                ...
                MOVE 99   TO numBMI_cat_high_break (1, 8)

In any case: Check if you really do want edited fields - the . within PIC 99.9 or if you want to use fields that can be used for calculation which would be PIC 99v9... and possibly have a look at DECIMAL-POINT IS COMMA in SPECIAL-NAMES if you want to use a comma instead of a period in decimals.

Note: You likely want to place both "fixed" and "variable" content in the same row, that would be:

           01 BMI_tabell. 
                05 tabell_columns_highbreak OCCURS 2 TIMES.
                    10 Cutoffhigh_rows      OCCURS 8 TIMES.
                        15 numBMI_cat_high_break     PIC 99.9.
                        15 BMI_meddelande_kort_cat_X PIC X(30).

BTW: To not look "alien" to most COBOL programmers: consider using hyphens - instead of underscores _ in user-defined names (language-wise both are OK, just stick to one).

Simon Sobisch
  • 6,263
  • 1
  • 18
  • 38
0

Technically, there is nothing wrong with the table layout, it simply isn't the way most COBOL programmers would lay out such a table. The easiest way to hard-code the values for that table is to insert a VALUE clause like I have done here with hard-coded-values.

   01 BMI_tabell. 
       05 tabell_columns_highbreak OCCURS 2 TIMES. 
           10 hard-coded-values PIC X(32)
       VALUE "16,017,018,525,030,035,040,099,0".
           10 Cutoffhigh_rows REDEFINES hard-coded-values
                   OCCURS 8 TIMES.
               15 numBMI_cat_high_break PIC 99.9. 
                
           10 BMI_meddelande_kort_rows OCCURS 8 TIMES.
               15 BMI_meddelande_kort_cat_X pic X(30).

For COBOL 1985 and later, VALUE clause, General rules (text from the 2002 standard),

9) A VALUE clause specified in a data description entry that contains an OCCURS clause or in an entry that is subordinate to an OCCURS clause causes every occurrence of the associated data item to be assigned the specified value.

In this case, hard-coded-values "is subordinate to an OCCURS clause" (for tabell_columns_highbreak) so both occurrences receive the same values.

Each four-character substring in the VALUE clause corresponds to one occurrence in Cutoffhigh_rows. Alternatively, hard-coded-values could have been followed by eight separate PIC and VALUE clauses, one for each occurrence in Cutoffhigh_rows.

Rick Smith
  • 3,962
  • 6
  • 13
  • 24
0

When using tables in COBOL, you define rows, then you define columns and you can have a very flexible data structure under the column definition. However, you can't have separate columns (that can be referenced by 2 subscripts) each with a different picture (Like SQL Tables for example).

COBOL allows you to nest levels. In your case, 1 level is required to represent the rows, and this row can be divided to 2 different fields.

The easiest way to initialize the table with user specific data is to write an explicit MOVE statements (as in the example below). You could also initialize the table in the WORKING-STORAGE SECTION but this requires very carful typing and the use of the REDEFINES keyword. This method is good when you want to store some data structures in a COPY BOOK used by different programs, say for state names, days of the week, month names, etc. You then use the REDEFINES method and write no code in PROCEDURE DIVISION. Of course you could load the data from files or database.

Here is a an example:

IDENTIFICATION DIVISION.
PROGRAM-ID. TABLE-TEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  R PIC S9(4) COMP VALUE ZERO.
01  S PIC X VALUE SPACE.
01  MYTABLE1.
    02 TROWS OCCURS 8 TIMES.
           05  COL-NUM  PIC 99V99.
           05  COL-TEXT  PIC X(30).
      
PROCEDURE DIVISION.

DISPLAY 'Test Method 1'.

INITIALIZE MYTABLE.
MOVE 16.0 TO  COL-NUM (1). MOVE "Some text for cell 1" TO  COL-TEXT (1).
MOVE 17.0 TO  COL-NUM (2). MOVE "Some text for cell 2" TO  COL-TEXT (2).
MOVE 18.5 TO  COL-NUM (3). MOVE "Some text for cell 3" TO  COL-TEXT (3).

MOVE 99.0 TO  COL-NUM (8). MOVE "Some text for cell 8" TO  COL-TEXT (8).

PERFORM VARYING R FROM 1 BY 1 UNTIL R > 8
                DISPLAY  R s COL-NUM(R) s COL-TEXT (R) 
END-PERFORM
.
STOP RUN.
NoChance
  • 5,632
  • 4
  • 31
  • 45