One of the solutions mentioned above is to use several programs or nested programs, for which I gave an example below, which is solution 1.
Another solution is the COBOL class, which may not be to your liking, but I like it, so I included an example, which is solution 2.
Solution 1:
program-id. TestProgram. working-storage section. 01 file-name pic x(128). 01 file-lines pic 9(9). procedure division. move 0 to file-lines move "d:\rts_win32.txt" to file-name call "program1" using file-name file-lines display file-lines stop run end program TestProgram.
program-id. Program1. file-control. select file-a assign to myfile organization is line sequential. data division. fd file-a. 01 file-a-line pic x(80). working-storage section. 01 EOF-A pic 9 value 0. linkage section. 01 lk-filename pic x(128). 01 CN-READ-A pic 9(9). procedure division using lk-filename CN-READ-A. move lk-filename to myfile open input file-a perform READ-A until EOF-A equals 1 close file-a goback. READ-A. READ FILE-A AT END MOVE 1 TO EOF-A NOT AT END ADD 1 TO CN-READ-A END-READ. F-READ-A. EXIT. end program Program1.
Decision 2
program-id. TestProgram.: working-storage section. 01 file-counter type FileLineCounter.
procedure division. set file-counter to new type FileLineCounter("d:\rts_win32.txt") display file-counter::LineCount stop run end program TestProgram.
class-id FileLineCounter.
file-control. select file-a assign to myfile organization is line sequential. data division. fd file-a. 01 file-a-line pic x(80). working-storage section. 01 cn-read-a binary-long property as "LineCount". method-id New. 01 EOF-A pic 9 value 0. procedure division using by value filename as string. set myfile to filename open input file-a perform READ-A until EOF-A equals 1 close file-a goback. READ-A. READ FILE-A AT END MOVE 1 TO EOF-A NOT AT END ADD 1 TO CN-READ-A END-READ. F-READ-A. EXIT. end method. end class.
source share