Creating and Saving Files

See all articles from T-S Horizons n1
Author(s): Bill Johnson
Publication: T-S Horizons
Pub Details: T-S Horizons n1 (Issue 1)
Date: November 1983
Pages: 20-22

One of the best features of the BASIC language that Sinclair designed into the ZX-81 (TS-1000) is its ability to SAVE all of its variables, including arrays, in the normal course of SAVEing the program onto cassette tape and reloading the variables along with the program LOAD. This allows us to transfer a file, in the form of data, along with the program to manipulate or interpret it, to and from tape in a single motion.

In this article, I would like to discuss the best ways to create, manipulate and SAVE/LOAD files on the ZX-81 and to point out the pitfalls as well as the opportunities of using it as a personal information storage system.

The questions of how to structure the file and what type of arrays to use will be considered later in this article. First I would like to outline the actual program steps that are essential to create and maintain a file.

A. Create File Array – having decided on the file size, dimension the array accordingly (i.e: DIM T$ (20,32)).

B. Delete DIMension Statement – since the file has been DIMensioned in step A, a re-dimensioning will never be required. Deleting this redundant statement saves file space.

C. Files – can be inserted/overwritten in the array at any time, even after the program has been SAVEd or LOADed.

D. SAVE program and files using SAVE statement in the course of running the program, with a GOTO statement following it, pointing to the beginning of the program, i.e.:

9050 PRINT "PRESS RECORD ON TAPE PLAYER"
9060 INPUT X$
9070 PAUSE 450
9080 SAVE ""
9090 GOTO 1

This enables the program to be self-starting when it is re-loaded from tape, eliminating a need for either a RUN statement, which erases the variables and thus the files, or a GOTO, which is cumbersome.

If a file program is structured this way and the files are set up accordingly, there is:

A. Full access to the files as set up.
B. Minimum memory usage (no redundant variable assignments or DIM statements).
C. Minimal chance of erasing files.

Arrays

As a general rule, the most efficient way to store file information, from both a program and variable standpoint, is by using arrays. Because the file information can be accessed by its location or address in an array, we process or access any piece of file information by referring to its address (i.e. T$ (20,2)). This allows us, for instance, to arrange information in a matrix, with a different type of information in each column, and a set of information in each row. The only alternative to using arrays is using a different variable for each file item. This process is so unwieldy as to be unworkable in all but the most limited cases.

File Format

The first step in deciding how you want to pack information into an array is to identify:

A. What information is important and pertinent?
B. What memory constraints do I have?
C. What display constraints do I have?

For example, in writing a program of my own to file and process budget/expenses data for every transaction in a quarterly period, I determined that, though I would have like to store more information, four items were critical: The Date – (mo/dy) of transaction, Remarks – to indicate the place and item(s), Transaction – CASH, VISA, check #, etc., and $Entry – the amount spent. I limited each data set to these four in this case primarily because of the constraint imposed by the display. I wanted to be able to list all of the information on one 32-character line, so as to be able to display a set of consecutive entries on the screen in a row/column format. The memory constraint is no problem, since I have 64K but you may have to be careful not to create files that are too large for the memory that you have available. In that case, you may have to trade-off file size against memory size.

To be more specific, the 32-character line of my budget program consists of the date (5 characters), Remarks (14 characters), Transaction (4 characters), $Entry (6 characters) and 3 spaces to separate the items. The act of setting the display format will force you to be specific about the type of information you need to store, and this in turn will give you a clear structure that can be translated into a programming array structure.

Options in Setting-Up Arrays

There are two sets of options to face in choosing an array format, the first of which is between numeric and alphanumeric variables.

1. Numeric/Alphanumeric

If the files you are storing hold primarily words and symbols, the string (alphanumeric) array is the simplest and best solution. You simply insert the file information into the array location ( i.e. LET R$ (20) = “KROGER-MEAT”). But if the file consists of words and numeric values (or entirely numeric values) there are schemes available:

A. You may use multiple arrays, storing words and numbers in alphanumeric and numeric arrays, respectively. This allows for the easiest, most straight forward manipulation of variables within the program. Each display line would be two or more separate array items printed sequentially. Thus a statement like PRINT D$(20);” “; R$(20); ” “; T$(20);” “; E(20) would yield a line like this:

10/25 KROGER-MEAT  5.26

Unfortunately the time consumed in searching for and printing all the items can become too great.

B. Or you can store all the data for one entry in a single alphanumberic string. This has several ramifications. If the numberical data is to be recalled and manipulated, then the function VAL must be applied to the substring after it is sliced (i.e., LET x=VAL T$ (20.5) (10 TO 14), would return the numberical value). Then to be reinserted, STR$ must be used (i.e., LET T$ (20,5) (10 TO 14) = STR$ x or something similar. The cumbersome nature of storing numeric values is compensated by the speed with which they can be displayed (a line can be printed with a simple command), and the clarity of having the variables stored in the same format as they are displayed. And if variable-length items need to be right-justified, then better to do it once in a string than every time an item is printed.

To continue the example of the budget program, the choice of array was dictated by:

1. The need to display several lines relatively quickly.
2. No need to change the files once they were in place.
3. Numeric values needed to be right-justified.

I therefore chose to store the entire file in a single string array variable T$, with each element (line) being 32-characters long. But if the bulk of the file you want to establish consists of numbers and they require continual re-definition, then, scheme A would probably be more appropriate.

2. File Dimension

The other set of options to face in choosing an array format is to set the number of dimensions of the array and its size. If the information structure has been worked out at this point the choice of how many dimensions you want the array to have should be quite simple. A one-dimensional array (like a single column of items) can be used for a single string of numbers or, as in my example, a single alphanumeric column of 32-character lines. A two-dimensional array (like a matrix of rows and columns) can store numbers in a row-column format. Using this information along with the chapter in the ZX manual on arrays, you should be able to define an array to hold your files.

The main thing to note about setting the file SIZE is that it is critical to DIMension the array (file) to the maximum forseeable size that will be required. This is akin to filling a file folder with enough blank sheets of paper to hold all of the information that you will want to write into the file. If the array turns out to be too small to hold all of the information you later want to store in it, there is no recourse but to re-DIMension the array (file) larger, and wipe out all of the information that you have already stored there.

Conclusion

The storage of file information on a ZX-81 can be very useful if the information is easily accessible and simple to INPUT. And it’s an excellent way to experiment with variable assignments and learn to manipulate arrays on the ZX-81.

(Editor’s Note: We invite comments and further articles on the subject of file creation and manipulation from our readers.)

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.