Search code examples
cobol

Cobol storing file to table


I'm trying to store a pattern received from text file into a table in COBOL. I am using READ.. INTO.. statement to do so, and here is what I have so far.

WORKING-STORAGE SECTION.
   01 ROWCOL.
        03 NROW     PIC 9(3).
        03 NCOL     PIC 9(2).
   01 PATT-INIT.
        03 ROW PIC X OCCURS 1 TO 80 TIMES
              DEPENDING ON NCOL.
   01 PATT.
        03 COL OCCURS 1 TO 80 TIMES
              DEPENDING ON NCOL.
              05 ROW OCCURS 1 TO 100 TIMES
                    DEPENDING ON NROW PIC X.

   PROCEDURE DIVISION.
   MAIN-PARAGRAPH.
        OPEN INPUT INPUT-FILE.
        READ INPUT-FILE INTO ROWCOL.
        PERFORM READ-PATTERN
        STOP RUN.

   READ-PATTERN.
        READ INPUT-FILE INTO PATT-INIT(1:NCOL).

The pattern in the input.txt would look something like this:

011000
001010
010100

The thing about this is that, I'm not sure how to place the PATT-INIT array into the PATT 2d-array. I'm only using the PATT-INIT array to receive row-by-row the pattern in each line. Then, I'm trying to store it into PATT 2d array such that I can access each number by the index numbers. e.g. PATT(1:2) would return 1.

Please give me some pointers on how to implement this. If READ.. INTO.. is not the way to go, I'm more than happy to receive other suggestions.


Solution

  • I think part of your problem is that you think things like (1:NCOL) are doing one thing, when in fact they mean something completely different. The notation indicate "reference modification". You probably are expecting ordinary subscripting, or at least "reference modification" from a variable starting point with a fixed length of one.

    01  a-nicely-name-table.
        05  FILLER OCCURS 80 TIMES.
            10  a-nicely-named-row-entry.
                15  FILLER OCCURS 6 TIMES.
                    20  a-nicely-named-column-entry PIC X.
    

    The data from your READ goes into a-nicely-name-row-entry ( subscripted ). Once everything is there, you can reference a paricular column on a particula row by a-nicely-named-column-entry ( a-row-subcript, a-column-subscript ).

    Note, without the ":" this is subscripting, not "reference modification". The comma is optional.

    You need to ensure that you don't go "outside" the bounds of the number of rows you put in the table, and also that you do not "overflow" the table with input data.

    You can use indexes for subscripting (INDEXED BY on the OCCURS definition). I haven't in the example, as it is unclear what you are trying to achieve.