Line Print Utility

See all articles from SYNC v2 n6
Author(s): Martin Albrecht
Publication: SYNC
Pub Details: SYNC v2 n6 (Volume 2, Issue 6)
Date: November/December 1982
Pages: 62

Reading computer-style text, where the words get chopped off at the end of the line, is easy enough once you get the hang of it. However, if the printout of a program is intended for use by people not used to this style, it is friendlier to PRINT out the text in a more legible format.

Listing 1 is a simple subroutine for the Timex/Sinclair computers that will PRINT a string text in such a way that the left margin is straight and each line ends with a whole word.

This rudimentary word-processing utility uses a pair of nested FOR-NEXT loops and exploits the unique string-slicing capabilities of the Timex/Sinclair.

Here is how it works. After the string text is registered, line 20 sends the computer to the 32nd element in the string. Since the Timex/Sinclair video format prints 32 columns across the screen, the 32nd element of the string will be at the right-hand margin of the first line of raw text. At this point (R), lines 30 and 40 start the computer counting backward (R minus K), inspecting the code of each successive string character. If code 0 (the space) is found, the machine jumps out of the K loop to line 60. Here it is commanded to PRINT the slice of A$ that starts with the 31st element counting backward from R (here, the first string element, since 32-31=1) and ends with the first space character it met on its backward search from R. This gives the first processed line of text.

The trick now is to make the machine print out the second and subsequent lines by the same rules.

Simply incrementing R by 32, thus sending the machine to the 64th string element will not do, except in the unusual case that the 32nd element happened to be a space. In the more common case, the machine has to “dump” one or more string elements in its backward count from element 32 until it encounters the first space. These “dumped” letters, of course, make up the first part of a chopped word, and they must be recovered to begin the second line of processed text. If we send the computer to string element 64 to begin its space search, we will have gone past the right-hand margin of the second line of text and be somewhere in the third line. Consequently, the letters dropped from the first line will be lost. Our starting point for searching the second line has to be not 64 (the 64th string element) but rather, 64 minus the number of dropped letters. This number is given by the value of the counter, K. Therefore, we decrement the value of R by K (line 70 of the program) and only then do we increment R by STEP 32 and start the cycle anew.

Listing 1. Line Print Utility.

  10 LET A$=" (YOUR TEXT TO BE PROCESSED) "
20 FOR R=32 TO LEN A$ STEP 32
30 FOR K=0 TO 32
40 IF A$(R-K)=CHR$ 0 THEN GOTO 60
50 NEXT K
60 PRINT A$((R-31) TO (R-K))
70 LET R=R-K
80 IF (R+32)>LEN A$ THEN GOTO 100
90 NEXT R
100 PRINT A$((R+1) TO )

Everything proceeds smoothly now until the program gets to the last line of the raw text. If, as is usually the case, this remainder has fewer than 32 elements, then the next STEP value of R would exceed its limit of LEN A$. The program therefore balks. It might be though that this could be fixed by raising the limit of R, say to the value LEN A$ + 32, but this does not work. Any element of A$ with a subscript higher than LEN A$ will not be found when the computer gets tp the PRINT command, and again we get error report 3—subscript out of range. The solution is line 80, which inspects the value of R to see whether the next incremental step would exceed LEN A$. If so, the machine is directed to line 100 with its command to print everything from R + 1 (the last space plus one) forward to the end of the string.

The ZX81 handles this slightly complex operation, in which the slicing subscripts are themselves expressions, with remarkable dispatch. It spits out the processed text at a rate of better than five lines to the second, in SLOW mode. It could be made to go marginally faster by limiting the value of K to, say, half the line (16) or even less, since there are few words of this length. But this does not seem necessary. If the string text contains a word of more than 31 letters, however, the program in its present form breaks down. If this is liable to be a problem, you can always add line

45 IF K=31 THEN PRINT A$

In other words, if the word is too long forget it!

This routine prints the text flush left, ragged right. There is a clean left margin without the leading spaces that occur in the raw text. If instead you want to print it flush right, ragged left, then enter the following lines in Listing 1:

60 PRINT TAB K; A$((R-31) TO (R-K))
100 PRINT TAB (LEN A$+1-R); ((R+1)TO#)

It should not be too complicated to add further modifications that run centered with both sides ragged or flush on both sides with the extra spaces distribution throughout the line.

By adding program line 15 PRINT A$ (and 16 PRINT for clarity) you will obtain a before-and-after comparison between the raw and the processed string.

This short utility routine shows that a computer need not be loaded with megabytes of memory to clean up its PRINT act. Of course, this routine cannot accomplish hyphenation of words. But for the modest purpose of making the PRINT output of the computer more legible to the user, it works just fine.

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