INTERMEDIATE SECTION F FILE INTER.F TABLES AND ARRAYS ***************** And now we come to a very interesting subject, but one that can be confusing unless steps are taken to understand it well.A large part of computing technique is concerned with the storage of data, and the use of arrays enables units of data to be stored and reproduced easily at will. The data can be provided in a variety of forms; numbers, words, symbols, Hex numbers etc. These pieces of data are stored in a set of a special type of variable known as a subscripted variable, which looks like this :- variable(subscript) As you see it is composed of two parts, main variable and subscript. The subscript is always a numeric variable since the subscript is always a number, but the main variable can be numeric or string, depending upon the type of data to be handled. A subscripted variable to store numbers might be num(y) where y would represent the quantity of numbers to be stored. The first piece of data would be stored in num(1), the second in num(2) etc. So much for the variable, now for the data. Data is written into the program in lines which start with the command DATA e.g 700 DATA 34,56,89,432,234,675,2,45,21,2 710 DATA 0,56,453,23,765,34,231,1,0,0 In the variable num(y), the value of y must be matched to the number of pieces of data to be stored. In the example there are 20 separate data items, so y = 20. We specify this near the start of the program with the DIM (dimension) command. we might write :- 40 DIM num(20) This reserves space in memory and must be included in the program unless an array of 10 or less items is constructed. We have notified the computer of our intention to construct an array of 20 numbers (DIM command), and have written in our DATA lines.What we have to do now is to store the data in the subscripted variables. This is done with a FOR...NEXT loop and the READ command as follows. 120 FOR y=1 to 20 130 READ num(y) 140 NEXT y So,to recap, we warn the computer of memory needs and introduce the variable, line 40. Then we arrange for the data held in lines 700 and 710 to be READ into the variable set, lines 120 to 140. The DATA lines can be situated at any convenient part of the program and the READ command will search forward through the program to find them and do the business. Once read, DATA lines are henceforth ignored, unless reactivated with the RESTORE command. e.g 200 RESTORE 700 ; when data lines from 700 onwards(700 and 710 in our example) are restored for use. The command RESTORE 710 would ignore line 700. Having RESTORED the data line or lines we would want to put them in an array. We would therefore need another FOR....NEXT,READ section and another variable. Using num(y) would be pointless as it is already full. Of course we could have read the RESTORED data lines into num(y) if y had been dimensioned to 40 and two loops (FOR y=1 TO 20, and FOR y=21 to 40 had been used) There are so many possibilities that I can only give a few without becoming too complicated. Try to follow the fundamentals and you will develop a general understanding which can be reinforced by experiment. Using the array. Now that the data is safely tucked away in the *************** variables it can be retrieved and used at any time by all the usual means e.g PRINT num(5) would display 234. We can also call up blocks of variables by using a FOR..NEXT loop. 2000 FOR a=4 TO 9 : PRINT num(a);: NEXT ========================= MULTI-DIMENSIONAL ARRAYS ========================= So far we have only considered a one-dimensional array, and this can be likened to a number of 'cells' in a line, each containing an item of data.. [ 23 ][ 14 ][ 11 ][ 65 ][ 0 ][ -5 ][ 34 ][ 12 ] Thus if the variable used is num(y), then num(2)= 14 , num(6)= -5 The only limit to the number of subscripts is practicality and store size. We can also use two-dimensional arrays, which can be thought of as a table of rows and columns. [ 45 ][ 5 ][ 87 ][ 90 ][ 23 ] [ 2 ][ 43 ][ 32 ][ 0 ][ 65 ] [ 34 ][ -7 ][ 83 ][ 20 ][ 9 ] This table represents a two-dimensional array with 3 rows and 5 columns and could be shown in a DIM statement as num(3,5). Therefore num(1,1)= 45, num(2,4)= 0, num(3,5)= 9 etc. Arrays with more than two dimensions can be thought of as two or more tables, the variables having three subscripts, the first specifying the table, the second the row in that table and the third the column in that table. Table 1 Table 2 [ 23 ][ 34 ][ 90 ][ 0 ] [ 65 ][ -76 ][ 32 ][ 54 ] [ 12 ][ -3 ][ 70 ][ 32 ] [ 5 ][ 33 ][ 45 ][ 44 ] [ 11 ][ 17 ][ 27 ][ -4 ] [ 19 ][ 550 ][ 55 ][ 22 ] In this case num(1,1,4)= 0; num(2,1,4)=54; num(1,2,3)= 70; num(2,3,4)= 22 etc. I have shown all the cells filled with numbers for clarity, but the items of data could be any form of data, remembering that if letters or strings are to be stored the variable must be a string variable such as t$(2,4,8). If a string variable is used, then numbers must be stored as strings for consistency. They can be recovered by use of the VAL function if needed. Multiple dimension tables are used when related groups of data are to be stored together. OPTION BASE You are given the choice of whether to use a subscript ********** of zero or 1 as the lowest subscript. The default is zero, and if this suits you don't bother to use this command. If you wish to change it, the commands are OPTION BASE 1 or OPTION BASE 0. Attempted use of both options in the same program is an error as is trying to change the option once an array exists. You may need it sometime to achieve compatibility whilst 'CHAINing' with another program. My advice is to ignore it until then. The listings which follow put what has been written above into practice. The listings are of necessity simple so as not to obscure the main principles. You are once again invited, nay, entreated to alter the listings after you have LLISTED them so as to find out what makes ARRAYS tick. In particular, take a pencil and paper and reconstruct the array tables as requested. You should find this interesting. If not, don't blame me, just kick the cat! End of file INTER.F find this interesting. If not, don't blame me, just kick the cat! End of file INTER.F