Is there a way to parameterize functions in COBOL?

I program procedures such as:

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. 

to read multiple files, and I was wondering if there is a way to code a procedure that can read the file name from a variable, so I don't need to code the same thing for each file. Thanks!

+4
source share
4 answers

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. 
+2
source

The right way for Cobol to parameterize routines is through a nested routine.

You can do what you want, but it depends on your compiler and environment, you can transfer a file, file name or DD name.

What platform are you working on?

Edit: On z / OS, you can change what FILE-A indicates at runtime using putenv () to configure the dataset name associated with the DDNAME that FILE-A uses.

See: http://ibmmainframes.com/post-57281.html http://cicswiki.org/cicswiki1/index.php?title=How_do_I_allocate_a_file_dynamically_using_COBOL%3F

You will need a pair of OPEN-A and CLOSE-A, as well as between switching files.

This does not exactly pass parameters to your read statement, but allows you to reuse OPEN / READ / WRITE / CLOSE instructions for different files. But only in series.

In VS COBOL II, there was a way where you could pass FD into a routine that would look something like this:

CALL MYREADPGM USING FILE-A CALL MYREADPGM USING FILE-B

This is possible with Enterprise Cobol, but IIRC VisualAge does not support this.

+1
source

It may not be "in the wild", but with compiler support, but the current ISO Draft 20xx standard includes a FUNCTION-ID instead of a PROGRAM-ID. It adds the paradigm of calculating function parameters to the COBOL function.

I can’t help today, but perhaps in the near future. If I am not mistaken, User-defined functions are actually from the COBOL 2002 specification, but it seems that the compiler developers fall into or do not support support for this function.

Support for FUNCTION-ID is in closed tests for OpenCOBOL 2.0, but the timeline for version 2.0 is not defined and may be another year or more before it becomes publicly available.

0
source

You can create a data file with file names, process them as a separate record, and then read each file. In "SELECT ... ASSIGN" you will need to use the working storage variable for the file name and move the value from the "file of file names" to it.

Since you are using VisualAge, I assume that on UNIX you can also run the program from the shell (sh, ksh) with the file name as a parameter and re-run the program from the shell for each file name.

-1
source

Source: https://habr.com/ru/post/1337358/


All Articles