REBOL for COBOL programmers

Go to table of contents Go to feedback page

FILE SECTION

Date written: October 4, 2012
Date revised:
Date reviewed:

This explains the REBOL equivalent of the FILE SECTION (if there is one).


COBOL FILE SECTION

In COBOL, the FILE SECTION is a section of source code where the files that the program reads or writes are defined. There is syntax to tell what kind of hardware is used (disk, tape, card reader, card punch, paper tape). For disk files, record length and blocking factor may be defined, as well as other features that are not really thought about much anymore (size of disk area, number of areas, etc.). In the glory days of COBOL, most files were fixed-length records with data items defined not by their format (as in REBOL) but by their positions in the record. So, for example, you could have

FD  TEST-FILE
    VALUE OF FILE-ID IS TEST-FILE-ID
    .
01  TEST-RECORD.
    03  TEST-PERSON-ID           PIC x(06).
    03  TEST-PERSON-NAME         PIC X(30).
    03  TEST-PERSON-CHILDREN     PIC 9(02).

With code like the above, you also would have an entry in the ENVIRONMENT DIVISION of something like

SELECT TEST-FILE
    ASSIGN TO DISK
    ORGANIZATION IS LINE SEQUENTIAL
    .
and then in the WORKING-STORAGE SECTION you would have something like
01  TEST-FILE-ID  PIC X(100) VALUE "testfile.txt".

The REBOL equivalent

In REBOL, there is no specific area where you define files. REBOL was designed for small programs, and it was not designed for the kind of file processing that was/is done in COBOL. Files are basically disk files, and most times they are read into memory entirely before processing (not always, just mostly). Records are not in a fixed format. Data is mostly free-format with the type of data being indicated by its appearance.

In COBOL versus REBOL, the choice of language is related to the choice of file format. In the fixed-format example above, a data record might look like this:

         1         2         3         4         5         6
123456789012345678901234567890123456789012345678901234567890
000100JOHN SMITH                    02                      
In a REBOL environment, you probably would not encounter a file like this example. The same data might look something like this:
"000100" "JOHN SMITH" 2
If you wanted to read or create a fixed-format file, it would be very easy to do it in COBOL and very hard in REBOL. If you wanted to read or create a free-format file, it would be very easy to do in REBOL and very hard to do in COBOL.

So, in COBOL, you define all the properties of a file including the format of the data. In REBOL, these very ideas don't even exist. So if you are used to doing all that definition in COBOL, you might feel the need to do it in REBOL. You might wonder what one does if one has to process some file. To make that transition easier, you can fake a file section.

This page is just showing a general approach to file processing and not the details, which will be covered in a later example. So to keep the scope of discussion small enough to be helpful, let's consider what you might do if you had to read a basic text file like the example above.

In COBOL, the coding of the FILE SECTION is an organizational tool that helps you collect your thoughts about files, and provides as an end result data names to refer to the fields in the data records. Let's do something similar in REBOL. Using the above fixed-format example, you could code, probably near the start of your program, something like this:

TEST-FILE-ID: %testfile.txt
TEST-FILE: ""         ;; whole file in memory
TEST-RECORD: ""       ;; one record
TEST-PERSON-ID: ""  
TEST-PERSON-NAME: ""
TEST-PERSON-CHILDREN: 0
TEST-REC-COUNT: 0     ;; number of current record
TEST-EOF: false       ;; end-of-file flag

The above coding, which is not necessary to get a working program, provides words that you can use to refer to the various parts of the file. But why stop there. In COBOL, the reading and writing of the file would be done in the PROCEDURE division, but in REBOL you can put that kind of code wherever you want. So, why not put it in the pseudo-file-section. For example (we assume that you have at least seen the coding or REBOL functions):

TEST-FILE-OPEN-INPUT: does [
    TEST-FILE: copy []
    TEST-FILE: read/lines TEST-FILE-ID
    TEST-EOF: false
    TEST-REC-COUNT: 0
]
The above code would be similar to a procedure to OPEN the file in COBOL, except that it reads the entire file into memory. I am making a little assumption here. In COBOL you have to "open" a file, and the coding would be
OPEN INPUT TEST-FILE.
I am assuming that you probably isolated that code in a paragraph so it could be performed from several places, or re-used, and that you coded some other items to help in the processing, like a flag to mark the end of the file, and a record counter to bump with each "read" to get a total when you hit the end.

The above code brings the whole file into memory, but how does one get at the individual data items? The result of the read/lines is that TEST-FILE refers to a block of text lines. In real REBOL, you would code a loop to go through the items of the block. That would be the "foreach" function, probably. But if it helps in your thinking, you could make a procedure to read a record at a time. For example, in COBOL you migh write

TEST-FILE-READ.
    READ TEST-FILE
        AT END
        MOVE "T" TO TEST-EOF
    .
and then you would PERFORM that paragrpah, probably at the end of a loop. You could do something similar in REBOL with
TEST-FILE-READ: does [
    TEST-REC-COUNT: TEST-REC-COUNT + 1
    TEST-RECORD: copy ""
    if none? TEST-RECORD: pick TEST-FILE TEST-REC-COUNT [
        TEST-EOF: true
    ]
    if not TEST-EOF [
        TEST-UNSTRING-RECORD
    ]
]

What the above code does is to bump the record count (set to zero when you "opened" the file), clear the record area, and then pick the first item in TEST-FILE, which will be a string. If the result of that "pick" function is true, then set the EOF flag to indicate that you have hit the "end" of the "file" so to speak. If you have not hit the end, then take apart the fixed-format string into its "data items" with the TEST-UNSTRING-RECORD function.

The function TEST-UNSTRING-RECORD will take apart the fixed-format data in TEST-RECORD and put the "fields" into the words set up for them, TEST-PERSON-ID, TEST-PERSON-NAME, TEST-PERSON-CHILDREN. We are not going to pursue this further because a later example will cover processing a fixed-format record.

To summarize the key concept, there is not FILE SECTION in REBOL but you can cluster all your file reading and writing in one area in your program to organize things after your own way of thinking.