Most programs were written to process everything sequentially, from beginning to end. The process of creating a disk file that could use the Indexed Sequential Access Method (ISAM) involved a great deal of preparation by assembly language system programmers, and in many cases, it was not worth the effort.
To allow easy random access of any file--be it on card, tape, or disk--IBM created the concept of the table file for RPG. (Did you know you can still access data from your tape drives directly from RPG IV without uploading the data first? But that's a topic for another day.) Essentially, the table file places the entire file into memory and treats it as any other table or array. Today, in RPG IV, all table and array functions are valid with table files.
There are two principle drawbacks to using table files: 1) You must describe the data within the program using I-specs, and 2) You must know how many records are in your file. This alone is enough to turn most programmers away from using table files. On the other hand, this technique does have advantages. Loading data into main storage creates lightning fast retrievals. This can be most useful for small tables that are used repetitively against larger files. You also have a great deal of flexibility in defining the fields that will be used for lookups. You can define alternate array entries that contain subfields. You can even define multiple elements per record!
The RPG IV manual for V5R2 refers to this method of array processing as "Prerun-Time Arrays." Additional information may be found by referring to that heading.
To code a table file, begin with the F-spec:
FMyArray it f 80 disk
Notice that the file must be program described with a record length specified. It is not necessary to use any keywords with a table file. This is new with RPG IV. It is advisable not to specify a key, even if the database is indexed. Doing so just adds unnecessary overhead to the program.
D-specs are necessary to define arrays:
DLookHere s 8a dim(100) fromfile(MyArray)
DGetThis s 72a dim(100) alt(LookHere)
This indicates that there are 100 records in the array and, therefore, 100 records in the file. The mandatory FROMFILE keyword tells the compiler to load data from the table file. To the best of my knowledge, the array will not automatically expand if there are more records in the file, but there will be blank array entries if there are less than 100 records in the file.
The final step in the process is the I-specs:
IMyArray ns 02
I........................Fmt+SPFrom+To+++DcField+++++++++L1
I 1 8 LookHere
I 9 80 GetThis
I 9 25 SubField1
I p 26 30 2SubField2
I 31 40 0SubField3
I 41 80 Etcetera
Any array function may now be performed in the calculations section. When the field GETTHIS is retrieved, all of the defined subfields are available for use.
Table files are best used if performance is an issue or if you are reading directly from tape. There is little call for this type of processing under other circumstances. On the other hand, the knowledge of table file processing is a valuable tool to place in your RPG tool belt.
David Abramowitz is an independent consultant who is never late to the table. He may be reached at
LATEST COMMENTS
MC Press Online