File: Varray, Node: Varray, Up: (Basic)top Virtual memory arrays provides high-speed, random access to files. The data is stored in a disk file, instead of the memory of the computer, so the user can quickly retrieve data. The principles of Virtual Array storage is similar to Record I/O in that the data in the disk file is in a contiguous sequence of 512-byte records. CREATING A VIRTUAL ARRAY DATA FILE When creating a virtual memory file, the user must first associate a disk file name with a channel designator from 1 to 12. This will connect the program to a disk file and all statement associated with this file must have this number. The statement that makes this connection is the OPEN statement. When creating a file, the user must open the file for output which means that data will be moved from the program's local storage to the file. For example: OPEN "FILE.DAT" FOR OUTPUT AS FILE #1% This statement will delete an existing file with the same name and extension and create a new file. To have a matrix of data in virtual memory, the user must declare it in a special form of the DIM statement. This special DIM statement has the following format: DIM #, The file channel number must corresponds to the internal file designator where the program has opened a disk file. The variable appears as it would in a DIM statement for a matrix in main memory. For example: DIM #1%, A$(50%,3%) Which set-up an array of 50 rows and 3 columns. (Actually 51 rows and 4 columns. There is a row 0 "zero" and a column 0. This column and row can be used to store special information in the array.) The user can also store floating-point constants, integer constants, and strings in virtual memory matrices. The user can also specify more than one matrix in a virtual memory file. For example: DIM #7%, A(100%), B%(50%), C$(25%) One of the problem a user might have when using virtual array storage is the storage of string variables. If the user does not specify a length, the system assumes a default length of 16 characters. However there are times when storing string variables that the length needs to be longer than 16 characters. So the program can define the maximum length, which varies from 2 to 512 characters which is the power of 2. (i.e. 2,4,8,16,32,64,128,256,512) Thus: DIM #4%, X$(10%)=128 Where the maximum string length will be 128 characters. The user should extend the file to its file length when creating the file. Because of the way this system stores the data, it is more efficient to extend the file to it's maximum file size. For example, if the user knows that up to 100 rows of data is needed for array A$, but the user has only 10 rows of data to start with, the user should dimension array A$ to have 100 rows when creating the file. A simple statement disconnects the channel between the program and the file. It's called the CLOSE statement. The user must use this statement to make sure the file is closed properly. For example: CLOSE #1% 10 OPEN "FILE.DAT" FOR OUTPUT AS FILE #1% 20 DIM #1%,D$(50,3) 30 INPUT "NUMBER OF EMPLOYEES",E 40 D$(0,0)=NUM$(E) 50 FOR I%=1% TO E 60 INPUT "NAME",D$(I%,1%) 70 INPUT "ADDRESS",D$(I%,2%) 80 INPUT "SALARY",S 90 D$(I%,3%)=NUM$(S) 100 NEXT I% 110 CLOSE #1% 120 END 10 OPEN "FILE.DAT" FOR INPUT AS FILE #7% 20 DIM #7%,D$(50%,3%) 30 E=VAL(D$(0,0)) 40 PRINT TAB(30);"EMPLOYEE REPORT" 50 PRINT" NAME ADDRESS SALARY" 60 Z$= " \ \ \ \ $$###.##" 70 FOR I%=1% TO E 80 S=VAL(D$(I%,3%)) 90 PRINT USING Z$,D$(I%,1%),D$(I%,2%),S 100 NEXT I% 110 CLOSE #7% 120 END 10 OPEN "FILE.DAT" AS FILE #10% 20 DIM#10%, D$(50%,3%) 25 E=VAL(D$(0,0)) 30 PRINT"CHOOSE ONE OF THE FOLLOWING" 40 PRINT " (1) CHANGE ADDRESS" 50 PRINT " (2) CHANGE SALARY" 60 PRINT " (3) TO END" 70 INPUT "INPUT 1-3",A% 80 IF A%<1% OR A%>3% THEN PRINT"INVALID NUMBER"\RESUME 70 90 IF A%=3% GO TO 400 100 INPUT "INPUT NAME";N$ 110 FOR I%=1% TO E 120 IF N$=D$(I%,1%) GO TO 150 130 NEXT I% 140 PRINT "INVALID NAME"\RESUME 30 150 ON A% GO TO 200,300 200 INPUT "INPUT NEW ADDRESS",D$(I%,2%) 210 GO TO 30 300 INPUT "INPUT NEW SALARY",S 310 D$(I%,3%)=NUM$(S) 320 GO TO 30 400 CLOSE #10% 410 END