REBOL for COBOL programmers

Go to table of contents Go to feedback page

READ

Date written: March 20, 2015
Date revised:
Date reviewed:

This page explains the REBOL equivalent of READ.


COBOL READ

COBOL can read and write files of different kinds, depending on the file's ORGANIZATION clause in the FILE SECTION. It can do sequential files where lines are terminated by line-feed characters. It can do what some compilers refer to as LINE SEQUENTIIAL where lines are terminated by line-feed characters AND the line length can be variable, like a free-format text file. It can do RELATIVE files and INDEXED files. Files can be ASSIGNed to various hardware types (disk, tape, card reader, card punch). When you READ a file you get one record brought into a "record area" which is redefined in different ways to show where data items are expected in that record, usually in a fixed format.

REBOL READ

REBOL is a bit more limited, being designed for networking and for small programs. Basically, REBOL handles the equivalent of text files of ORGANIZATION SEQUENTIAL and does it by reading the entire file into memory. A COBOL programmer's reflex might be to consider that inefficient, but remember that in 2015, computers have gigabytes of memory. One does have the problem of writing a multi-user program where with that arrangement, but for the multi-user situation REBOL can work with ODBC databases through a mechanism that is not handled with the READ function.

The "read" function brings an entire file into memory, in different forms depending on the refinements of the "read" function. Assume have a test file like this:

TEST FILE LINE 1
SECOND TEST FILE LINE
THIRD LINE IN TEST FILE
and let's read it in some different ways. What we usually do is specify a word to refer to the entire file in memory, and then obtain a value for that word by reading the file.

Here is a basic "read" operation and some tests to see what the result is.

>> BASIC-READ: read %rtest.txt
== {TEST FILE LINE 1
SECOND TEST FILE LINE
THIRD LINE IN TEST FILE
}
>> type? BASIC-READ
== string!
>> print BASIC-READ/1
T
>> print BASIC-READ/2
E
>> length? BASIC-READ
== 64
Notice that the basic "read" function produces a string of characters that is the contents of the file. A sting is one of the "series" data types, so the various series functions work on it. The first item in the series is just the first character in the file.

Here is another common way to read, which is to treat the file as a bunch of lines, and not a bunch or characters.

>> LINES-READ: read/lines %rtest.txt
== ["TEST FILE LINE 1" "SECOND TEST FILE LINE" "THIRD LINE IN TEST FILE "]
>> type? LINES-READ
== block!
>> length? LINES-READ
== 3
>> print LINES-READ/1
TEST FILE LINE 1
>> type? LINES-READ/1
== string!
>> print LINES-READ/1/1
T
>>
Notice that this time the result of "read" is a block of lines. Each line in the block is a string. Since a block is a series data type, and a string is a series data type, one can refer to characters within strings with the "path" notation shown above for the first character of the first line. Another significant point about the "lines" refinement is that a line of a text file can contain REBOL values and those values can be recognized by a program based on their format. So, for example, if a line of text had a name and a birthdate and a dollar amount, that line could be parsed and the individual data items would be recognized without any fussing.

If you want to bring into memory something that is not text, like an image file, you would use the "read/binary" refinement.

The file name in a "read" operation does not have to be just a file on the local computer. It can be a url, which can be used to get a file from the internet, or from an ftp site. That is one of the useful features of REBOL, that it works smoothly with data over the internet.

There are other refinements of the "read" function. Theoretically, one could think of a file as just a big chunk of characters, and then go after a sub-chunk at some location inside the file, and make one's own form of relative or indexed file. However, one should first consider some of the features of REBOL that make it special. You could make a data file in the format of a text file. One line of text could be one record. The record could contain data items in REBOL's many recognized data types. Those features give a lot of power. It might be better to take advantage of those features, instead of bit-fiddleing one's way through a big chunk of characters just because one can. Save the bit-fiddling for when there is not other way.