Question

I'm trying to read a sequential file and write it to other file (with a different structure) using a COBOL program.

The structure of the old file contains a field that includes a table (an array).

So instead of using "move field-old to field-new" I need to write a loop for this table. How do I do that?

Was it helpful?

Solution 2

PERFORM VARYING MY-SUBSCRIPT FROM +1 BY +1 
        UNTIL MY-SUBSCRIPT NOT LESS THAN NUMBER-IN-FILE-RECORD
    MOVE  IN-FILE-FIELD1(MY-SUBSCRIPT) TO OUT-FILE-FIELD1
     .....
    MOVE  IN-FILE-FIELD99(MY-SUBSCRIPT) TO OUT-FILE-FIELD99
    WRITE OUT-FILE-RECORD
END-PERFORM.

OTHER TIPS

In you input-record you will have something like this:

01  a-nicely-named-record.
    05  nicely-named-data PIC ...
    ...
    05  and-a-final-nice-name PIC ...
    05  field-containing-the-count-of-actual-entries ... PIC ...
    05  the-COBOL-table.
        10  table-entry OCCURS n TIMES.
            15  table-entry-data-nice-name PIC ...
            ...
            15  table-entry-a-final-nice-name PIC ...

Something like that. It is better if you can show your own layout. The ... means "put here what you actually have, because we don't know the details". You input-record should have a prefix for all the field-names, but may not.

Your output-record:

01  a-nicely-named-record.
    05  nicely-named-data PIC ...
    ...
    05  and-a-final-nice-name PIC ...
    05  field-containing-the-count-of-actual-entries ... PIC ...
    05  the-COBOL-table.
        10  table-entry OCCURS n TIMES.
            15  table-entry-data-nice-name PIC ...
            ...
            15  table-entry-a-final-nice-name PIC ...

Again, there should be a prefix for all these. But there may not. You say the structure is different, but you haven't shown, so going for "logically" equivalent.

To access a table, you need to use subscripting. Subscripting is coded inside ( and ). Subscripting may be with a literal value ( 1 ), ( 2 ) etc, or with an index (defined as INDEXED BY on the data definition of the OCCURS) or by using a data-name as a subscript.

01  the-subscript BINARY PIC 9(4).

BINARY PIC 9(4) can contain a maximum value of 9999, if your table is bigger (unlikely, but not impossible, for a table on a file) then make it BINARY PIC 9(8).

PERFORM
  VARYING the-subscript
    FROM 1
    BY 1
    UNTIL ( the-subscript 
           GREATER THAN field-containing-the-count-of-actual-entries )
    MOVE input-field.... ( the-subscript ) to output-filed... ( the-subscript )
    ...
    MOVE input-field.... ( the-subscript ) to output-filed... ( the-subscript )
END-PERFORM

Using an index would be the same as the above, except by referencing the index-name instead of the-subscript.

If the number of entries in your table is small (a relative term) you can just use literals:

MOVE input-field.... ( 1 ) TO output-filed... ( 1 )
...
MOVE input-field.... ( 1 ) TO output-filed... ( 1 )
MOVE input-field.... ( n ) TO output-filed... ( n )
...
MOVE input-field.... ( n ) TO output-filed... ( n )

If the format of the table is the same on both files and you are sure that the data in the input table conforms exactly to the PICture clauses, it can be done with one MOVE statement.

MOVE input-table TO output-table

If there are a fixed number of entries on all records, you can use

initial value for subscript/index
PERFORM n TIMES
    MOVEs (as above)
    increment subscript/index
END-PERFORM

For further clarification if you have problems, update your question with the input and output records, the name of the compiler and the OS that you are using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top