home *** CD-ROM | disk | FTP | other *** search
- Using the BASIC Field Statement
-
- Bill Consiglio
- Boca Raton IBM PC Users Group
-
- The FIELD statement is used to
- allocate space for variables in a
- random file buffer. Having seen a few
- of the mistakes programmers have made
- with the FIELD statement and having
- answered some of their questions, I
- submit the following comments and
- hints on the subject.
-
- The most common mistake I've seen
- involves neglecting to use arrays in
- the FIELD statement. All the examples
- in the manual show discrete variables
- named in the FIELD, whereas in many
- programs the advantages of array
- processing can greatly simplify the
- design and code. The FIELD statement:
-
- FIELD #1,8 AS R1$(1),4 AS R1$(2),4 AS
- R1$(3)
-
- can be processed by the statement:
-
- FOR J=1 TO 3: LSET R1$(J)=R$(J):
- NEXT J
-
- This simplistic example becomes more
- meaningful as the process within the
- loop gets larger and would have to be
- re-coded for each unique variable in
- the FIELD statement. Many programs,
- for instance, fill in the buffer
- variables by prompting the user with
- input requests. This can be done
- nicely in a loop.
-
- The following example illustrates
- several points:
-
- 10 OPEN "TEST" AS #1 LEN=100
- 20 ' name address city
- 30 FIELD #1,20 AS R1$(1),20 AS
- R1$(2), 15 AS R1#(3)
- 40 FOR J=1 TO 3: LN=LN+LEN(R1$(J)):
- NEXT J
- 50 ' state zip phone
- 60 FIELD #1,LN AS DUMMY$,2 AS R1$(4),
- 5 AS R1$(5),8 AS R1$(6)
- (etc.)
-
- The comment lines (20,50) serve as
- reminders about the meaning of each
- field variable.
-
- FIELD statements, by their very
- nature, tend to get quite long. They
- can be kept to a manageable length by
- breaking them up into several FIELD
- statements. The trick to this is
- including the total length of all the
- preceding fields in a "dummy"
- variable at the start of the next
- continued FIELD (as in line 60). The
- length of the "dummy" field can be
- inserted manually during coding or,
- as in the example, it can be
- calculated at execution time (line
- 40). This procedure can be repeated
- for as many FIELD statements as are
- required.
-
- The next example does the same field
- definitions as the previous one. It
- uses a DATA statement (line 40) to
- define the prompt and length for each
- field.
-
- 10 OPEN "TEST" AS #1 LEN=100
- 15 FOR J=1 TO 6
- 20 READ PROMPT$(J),FLDLEN
- 25 FIELD #1,LN AS DUMMY$,FLDLEN AS
- R1$(J)
- 30 LN=LN+FLDLEN
- 35 NEXT J
- 40 DATA Name,20,Address,20,City,15,
- State,2,Zip,5,Phone,8
- 45 CLS
- 50 FOR J=1 TO 6
- 55 PRINT PROMPT$(J);TAB(15);" - [";
- SPACE$(LEN(R1$(J)));""";
- 60 LOCATE ,19,1: LINE INPUT "",Z$
- 65 LSET R1$(J)=Z$
- 70 NEXT J
-
-
- (etc.)
-
- In addition, line 55 makes use of the
- length of each field to show the user
- how many characters can be typed in
- the field. For example, the first
- prompt appears on the screen as:
-
- Name -[_ ]
-
- In this simple program, the brackets
- do not enforce the string length,
- that is, a user can type something
- longer, and a backspace will cause
- the ] to move left, but both of these
- minor problems can be fixed with a
- little more code.