SuperBasic Tutorial: Part 3 (or maybe 4)

See all articles from Sinc-Link v9 n6
Author(s): Howard Clase
Publication: Sinc-Link
Pub Details: Sinc-Link v9 n6 (Volume 9, Issue 6)
Date: November/December 1991
Pages: 13-14

I’m sorry there wasn’t an episode of this saga in the last issue of SincLink. I suppose it’s my fault for trying to get ahead and sending in two episodes at once; I won’t make that mistake again! But at least you got double measure in the previous one. I should also apologise for an error in part 1 – all references to line 210 actually should have referred to line 220 in the final version of the program that accompanied the listing. I must have got my versions mixed up somewhere. Since I have received no complaints about this (or any other comments apart from Hugh’s in the last issue!) I suppose that either no-one read it or everyone who did realised what I had done.

Listing 3-4 Upgrade

 100 nm$ = "Prt_dir4"
110 REMark hjc 1991.09.09 Ver 0.42

165 WIDTH#3,chars%

168 abc%=1: REMark Alphabetical flag

770 LOCal a$,c%,i,t%: c%=ch%+1

830 t%=cols%*rows%
840 DIM ddir$(t%,nl%)

905 IF abc%: Shellsort ddir$(TO n%)

1120 REMark ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1130 DEFine PROCedure Shellsort (a$)
1140 REMark Ascending order.
1150 LOCal gap%,i,j,k%,n%,sort,w$
1160 n%=DIMN(a$): gap% = n% DIV 2
1170 REPeat sort
1180 FOR i=1TO n%-gap%
1190 FOR j=i TO 1 STEP -gap%
1200 k%=j+gap%
1210 IF a$(j)>a$(k%)
1220 w$=a$(j): a$(j)=a$(k%)
1230 a$(k%)=w$
1240 ELSE EXIT j: END IF
1250 END FOR j: END FOR i
1260 gap%=gap% DIV 2
1270 IF gap% = 0: EXIT sort
1280 END REPeat sort
1290 END DEFine Shellsort

At the end of part 3 I left you with the cryptic comment “Afferbeck Lauder”. (This is the pen name of the author of a famous book on the pronounciation of English in a country where they celebrate Christmas in mid-summer.) The one difference between our listing of file names and the telephone directory is that they were still in random order. This is readily remedied since there is a useful collection of ready made PROCs in a program available from the QL library which contains the PROCedure “Shellsort” (nothing to do with conchology, it was invented by a Mr Shell.) All we need to do is to add this to the end of our program and include the appropriate call. This can be achieved (as long as you haven’t RENUMBERed anything) by typing in the Listing 3-4 upgrade, saving it in a temporary file, loading Prt_dir3 and then MERGEing the upgrade on top of it. This should produce Prt_dir4. Do NOT try to run the upgrade alone, it is not a complete program! (If you are lost don’t worry, I’ll give the complete listing in the end, and send it to the library.)

The first two lines bring the version info up to date. Line 168 introduces a new global variable abc% which is a flag to switch the alphabetical ordering on and off. Set it to zero to switch alphabetical order off and save time; any other value is treated as “true” in line 905 and the call to Shellsort goes ahead. I put the option in since the ordering will take a significant amount of time, especially if you have a diskful of files – but that is of course where it is most useful.

A few lines in the PROCedure Make_array had to be altered too. If you sort the whole array and it isn’t a full rectangular one, the empty elements, which we want at the bottom of the right hand column get shifted to the top of the left. To sort only the first part of the array which actually has file names in it required the introduction of a new local variable t%, replacing n% in a couple of lines of version 0.3.

Shellsort comes straight out of the library, except that I have removed a few REMs and adjusted line lengths to fit the narrow columns I am using in this article. I adapted it ages ago from a version in some sort of lesser BASIC and cannot remember for the life of me exactly how it works, but it does! As far as I remember it is quite fast and doesn’t require a lot of memory, this was more important in the days when 64k was a huge amount – we QLers shouldn’t have to worry! To reverse the order change the > in line 1210 to a <. It will work on string “slices” (e.g. fred$( 3 to 67) but there is one minor bug in the procedure if you do. If you send a string sliced at the beginning (eg fred$(3 to 67) it treats the first element (fred$(3)) as a$(0) and ignores it, so if you want fred$(3) to be included in the sort you have to send fred$(2 to 67) and let it ignore element 2 instead. It’s all to do with people counting from 1 while computers start at 0! In line 905 our array is only sliced at the end, so we don’t need to worry here.

When I experimented with smaller typefaces I found that it was necessary to make one other small addition to the program – the WIDTH command in line 165. This sets the line length on the serial port. The default is 80, so this only matters if you are trying to save even more trees by typing your listing in elite or condensed type. You will also have to instruct the printer with the appropriate control codes. If your printer is Epson compatible then the following trick should work:

PRINT#3,"ûM";:          REMark Elite type
PRINT#3,"ë";: REMark Condensed

The û character is produced by <CTRL+;> and ë by <CRTL+/>, if you think you can read condensed elite then use both (“ûMë”) but it will be very small and you will need very good eyesight!

Slip this in somewhere soon after OPENing channel #3. (See my article in QL World April 1991 for more details.)

There is also a bug in the QL’s string comparison routine, it treats upper case letters as “earlier” in the alphabet than the corresponding lower case one. So, for example “CHports_doc” comes before “Change_ports”, so you only get “sort of” alphabetical order – but at least all the “A”s, “B”s, “C”s etc come together. My first thought was to convert all the names to the same case before the comparison.

Type in the following few lines and MERGE them ON TOP of version 0.42 in the usual way. (Lower$() comes from the PROCs library too.) A small alteration to Shellsort is needed to call Lower$(). Note that only the sorting is done using the lower case version of the name; it is not changed in the listing. Don’t loose your copy of version 0.42 though, you will find that it slows up the sorting routine enormously. It might be tolerable if you are using mdvs, but could take minutes to deal with the directory of a floppy disk. I doubt whether many people will think it worthwhile!

 100 nm$ = "Prt_dir4b"
110 REMark hjc 1991.09.11 Ver 0.42b

1155 LOCal p$,q$

1205 p$=Lower$(a$(j))
1207 q$=Lower$(a$(k%))
1210 IF p$>q$

1300 REMark ^^^ h.j.c 1989.02.14 ^^^^
1310 DEFine FuNction Lower$(a$)
1320 LOCal i,l%,c,t$:t$=a$:l%=LEN(t$)
1330 FOR i = 1 TO l%
1340 c=CODE(t$(i)): SELect ON c
1350 = 65 TO 90 : t$(i)=CHR$(c+32)
1360 =160 TO 171: t$(i)=CHR$(c-32)
1370 END SELect : END FOR i
1380 RETurn t$: END DEFine

This route is obviously a dead end, it is just too slow. I shall go back to version 0.42 for my next step. I included it here as I am trying to illustrate the actual process of writing a program, sometimes you do have to backtrack! I’ll show you how to do it with a new extension in a later article.

Last time I left you with a couple of problems, both involving adaptation of the FuNction Line_count that starts at line 930. As written it returns the number of lines in any file – we used it to count the number of file names in a directory. I suggested that you might alter it so that it also returned the length of the longest file name i.e. the longest line in the file. Note! I said “also” – you cannot do that since a function can only return one value can you? Well yes you can! Using a little used trick, which is in fact documented in the manual. Look at the following listing:

  10 REMark m% is the length of the longest
20 REMark line in the file.
30 PRINT Count(4,"mdv1_Prt_dir3",m%)
40 PRINT "Longest line ="!m%
930 REMark ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
940 DEFine FuNction Count(ch%,f$,mx%)
950 LOCal a$,ct,n%,l%: n%=0: mx%=0
960 REMark Count no. of lines in file
970 OPEN#ch%,f$
980 REPeat ct
990 IF EOF(#ch%): EXIT ct
1000 INPUT#ch%,a$: l%=LEN(a$)
1005 IF l%>mx%: mx%=l%
1007 n%=n%+1
1010 END REPeat ct
1020 CLOSE#ch%
1030 RETurn n%: END DEFine

Note that the variable m% in line 30 which appears to be a FuNction parameter is undefined on entering the FuNction. It is given a value as its alias mx% within the FuNction at line 1005 – although I had to set mx% to zero earlier (950) or the comparison at 1005 would generate an error message. Any time the loop meets a longer line the value is increased accordingly. After the FuNction has returned its value m% has a value and can be printed in line 40.

The other problem was to convert it return the length of a file in bytes. This is easier:

  10 PRINT Byte_Count(4,"File_name")
930 REMark ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
940 DEFine FuNction Byte_Count(ch%,f$)
950 LOCal a$,ct,n%: n%=0: mx%=0
960 REMark Count no. of lines in file
970 OPEN#ch%,f$
980 REPeat ct
990 IF EOF(#ch%): EXIT ct
1000 a$=INKEY$(#ch%,-1): n%=n%+1
1010 END REPeat ct
1020 CLOSE#ch%
1030 RETurn n%: END DEFine

As you will see, only line 1000 has to be altered, INKEY$ reads a byte at a time, while INPUT takes in a whole line. If you have Toolkit II then the WSTAT command does this much quicker – by reading the value from the file header!

Something to think about for next time. Write a FuNction that checks if a single character is a digit within a specified range (e.g. 3-6) and returns TRUE (1) if is is and FALSE (0) otherwise.

Howard Clase,
Box 9947, Station B,
St. John’s, Nfld,
Canada A1A 4L4.
© 1991 H.J.Clase

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