CHAPTER IX

A valid point is that entering data through DATA statements is a 
cumbersome process.  If the data statement had to be altered each 
time to change some information, computers would lose a lot of 
their usefulness.  Fortunately data files can be put on a disk or 
a tape.

From the membership list started in chapter VIII, a data file can 
be created.  There are commands that permit the computer to do 
that.  The file system is patterned after the APPLE system.  If 
chr$(4), a heart, is printed in the first column internally to  the 
computer, it knows that the following command is directed at  the 
Operating System.  The command will be in quotation marks.   The 
question is "How to get that heart printed?" It is done by  first 
setting a string equal to chr$(4).  If d$ is reserved for 
operating system commands, we will always recognize the  situation.

File commands are OPEN, WRITE, READ, CLOSE, APPEND.  Using the 
program called NAMES, a file MEMBER can be created from that  data. 
 The information was put on the seven members in an array  we 
called me$(100,5).  Start by assigning d$ = to chr$(4), that's  how 
to get the heart signal.  After the data is in the me$-array,  OPEN 
a file.  The OPEN command must be in the same print 
statement as the heart.  It tells the computer to get the file 
known as MEMBER ready.  Tell it to WRITE information to that file 
(put information in to the file).  After the WRITE instruction, 
the computer knows that it will send all print statements to the 
file rather than the monitor.  When the data has been transferred 
CLOSE the file.  If the file is not CLOSED the balance of the  TAPE 
or Disk will not be useable -- the computer will think that  it is 
full of MEMBER information.  Make the following additions  to the 
program for membership list:

 15 d$ = chr$(4): n = 7
 200 ?d$; "OPEN MEMBER"
 210 ?d$; "WRITE MEMBER"
 220 ? n: REM puts number of entries on the tape
 230 for i = 1 to n: for j = 1 to 5
 240 ? me$(i,j): REM the data are being printed onto the medium
 250 NEXT:NEXT: REM All data now transferred
 260 ?d$; "CLOSE MEMBER"

Now when the program is RUN, the computer will transfer the data 
to the tape (or disk).  The computer can be used for other tasks, 
or turned off and the data is safe.

SAVE the above program as keepfile.

Put ADAM in the SmartWriter mode and have it GET the file MEMBER. 
It can be seen like any other note, letter, etc.  on the monitor.

A program is needed that will get the data back out of the MEMBER 
file and enable it to be utilized.  First, get the data back into 
the computer.  Here the order is different.  READ the file and 
INPUT the data into an array.  When the computer finds the heart 
in the first column and then the word READ in the print  statement, 
all INPUTS, until further notice, will come from the 
tape or the disk rather than the keyboard.  After finishing, the 
file must be CLOSED.  The program would look something like this:

 10 REM getting data from file
 20 dim me$(100,5)
 30 d$ = chr$,4)
 40 ? d$; "OPEN MEMBER"
 50 ? d$; "READ MEMBER"
 60 Input n
 70 for i = 1 to n: for j = 1 to 5
 80 INPUT me$(i,j)
 90 NEXT:Next
 100 ? d$
 110 ? d$; "CLOSE MEMBER"
 120 speed = 50: REM demonstrate that data now in RAM
 130 for i = 1 to n: for j = 1 to 4
 140 ? me$(i,j): next: ? : next
 150 speed = 255
 500 end

There are some important points in this program.  Note in 
statements 60/70, first a value for n was obtained and then used 
in the next statement.  This is a time saver as it is not 
necessary to look through the whole 100 possible entries that the 
array provides for if that many have not been made.

Second, statement 100 contains nothing but an instruction to  print 
d$.  That instruction is necessary, in the first column, in  order 
to get the computer to quit looking at the storage medium  for 
future inputs.  IF THE PROGRAM DOES NOT HAVE IT AND HAS  ANOTHER 
STATEMENT WITH INPUT OR GET, AN ERROR MESSAGE WILL BE  RECEIVED.

Now RUN the program.  A heart will be seen.  To some, these marks 
are a sign that everything is going well and the data is being 
input.  To others, they are a nuisance.  There are two ways of 
eliminating these characters on the screen.  LIST statements 60 
and 80 and make the following changes:

 60 INPUT " "; n
 80 INPUT " ";me$(i,j)

In the discussion of the INPUT command, there was a note that a 
question mark would be returned if a prompt was not used.  While  a 
space does not print out, the computer still looks at it as a 
prompt.  Make those changes and RUN the program again.

The second way is to revise 60, 80 and 100 to read:

 60 INPUT n: ? chr$(8);
 80 INPUT me$(i,j):? chr$(8);:?
 100 ? d$: ? chrs$(8)

Chr$(8) is the backspace character and in the process, it erases 
anything that was displayed in the previous space.  Type those 
changes and RUN again.  

SAVE the above program as RETRIEVE.  It will used again.

There is a CATCH 22 in these filing systems -- not just ADAM's. 
The first time a program that calls for retrieving data from a 
file, where is the file? There is none yet unless steps have been 
taken to create one.  In this exercise we have solved the problem 
by first writing and RUNning a program that created the file 
MEMBER.

With ADAMCalc, the problem may be solved by first putting data in 
a single column in the spread sheet (or in a block of cells if  the 
data are symmetrical).  Now STORE the data with ADAMCalc  using the 
VALUES ONLY MODE and calling it the same name to be 
used in the SmartBasic program.  SmartBasic can call and use the 
file and it will pick up the data in the order it was in the 
spread sheet.  Look at the file in the SmartWriter mode to see  how 
it was formatted.  If a block format was used, the data will  be in 
the file starting with the first row, leftmost column and  reading 
to the right.  After completing the first row, it drops  to the 
second row, left column and repeats.  This will be 
familiar as it is the nested loops in an array statement.

There is still another way.  In a multiple purpose program, ask  if 
data is available on tape or disk.  If the answer is, "No"  skip 
the part of bringing data from storage and start adding data  in a 
manner similar to the addname program.  This route also  provides 
for using different media in the future and gives a more  self 
contained program.

Now, a short program will be prepared so that a name can be added 
to the file.  Flexibility is needed if the file is to be 
increased.  The reasoning behind this operation is to get the 
names into RAM from the file.  That can be done by using the 
program RETRIEVE except delete statements 120 through 150.  Now 
add these statements:

 120 HOME: ? "Add new member.": n = n+1: REM we have increased 
  membership by one
 130 Input "Family Name? "; me$(n,1)
 135 INPUT "Given Name or Initials? "; me$(n,2)
 140 Input "Street Address? "; me$(n,3)
 150 Input "City? "; me$(n,4)
 160 Input "State? "; me$(n,5)
 170 ?:? "So you wish to add another - y/n?": get q$: if q$ = 
  "n" then goto 200
 180 ?: n = n+1: goto 130
 200 ? d$; "OPEN MEMBER"
 210 ? D$: "WRITE MEMBER"
 220 ? n
 230 for i = 1 to n: for j = 1 to 5
 240 ? me$(i,j): next:next
 250 ? d$; "CLOSE MEMBER"

Try running this program and add two or three more members from 
the directory.

Files can be created with numbers, strings, etc.  Sometimes it is 
beneficial to convert all numbers to strings through use of the 
command me$(i,j) = str$(x) using variables as appropriate.  This 
gives better control on the number of bytes used and the number 
can be conditioned without getting all the 999999s etc.

To utilize this information on files and arrays, alphabetize the 
membership list and then save that.  Part of the program, you 
should ($) write, but some steps will be suopplied to do the 
sorting.  First, get the data from the MEMBER data file and put  it 
in the array me$( ).  Don't forget to get the value of n.

There are several programs and theories on sorting.  The  following 
one is direct and quick in that it only has to massage  the list n 
- 1 times regardless of the original order.

Since sorting is to be done on the basis of name, or columns 1  and 
2 in the array, set a variable equal to the first name on the 
list.  Work down the list.  If the next name is greater than the 
first, just skip to the third.  If it is less, then reset the 
variable and remember the line number where the lower order name 
was found then go to the third etc.  After going all the way down 
the list, the lowest valued name, or the one that should come 
first in our list will have been identified.  If the sixth name 
down was the lowest, the name, and all other information will be 
put in the zero row of the me$(array).  The first name and  address 
information will be moved down to the sixth row, and then  the 
information in the zero row will be moved to the first row. 
Repeat the process starting with the second name on the list -- 
the first by definition is already the least!  Repeat this  process 
for n-1 times and then the list is sorted.  Since both  the data 
retrieval and data storage steps are being written, 
assign the sorting to a subroutine beginning at 5000.  At the 
appropriate time, place the instruction GOSUB 5000 in your 
program.

 5000 REM name sorting
 5010 for c = 1 to n-1
 5020 st$ = me$(c,1)+me$(c,2): kx = c
 5030 for xy = c to n
 5040 if me$(xy,1) + me$(xy,2)< st$ then st$ = 
  me$(xy,1)+me$(xy,2): kx = xy
 5050 next xy
 5060 for j = 1 to 5: me$(0,j) = me$(kx,j): next j: REM stores 
  lowest in zero row
 5070 for j = 1 to 5: me$(kx,j) = me$(c,j): rem makes room for 
  the lowest
 5080 for j = 1 to 5: me$(c,j) = me$(0,j): rem puts lowest 
  remaining in the appropriate place.
 5090 next c
 5100 RETURN

After that has been typed, go to a slower speed, say speed = 25  in 
the program and have the names in their new order printed out  on 
the screen.  After that is done, create a pause by using the  GET 
command and a "Hit any key statement", bring the speed back  up to 
255.  Having done all that, now write the steps to restore  the 
data in the file MEMBER on the disk or tape.

There is one last program that might be wanted -- one that will 
let the program delete a member.  Once again, get the data out of 
the file and put it in RAM.  Slowly print out the number and the 
name of the individual so it can be determined which row the 
person is in that the program is to delete.  Starting from that 
row number, move all in the information in the row immediately 
below the one wished to be deleted into the row being deleted. 
Continue down the list until all have been moved up one.  (If 
number 7 is being deleted, number 8 moves up to 7, 9 up to 8, 10 
up to 9, etc.).  This can be done with a FOR/NEXT series 
statements where the first number is the one to be deleted and  the 
last the number n.  When this is finished, subtract one from  n. 
Now print out the names slowly to see if the program 
accomplished what it set out to do.  Also see if the addresses  are 
right.  If they are, restore the data again in the MEMBER  data 
file.


 