home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MISC / TNH_PC.ZIP / CONSIGLO.NL < prev    next >
Encoding:
Text File  |  1987-01-14  |  3.1 KB  |  118 lines

  1. Using the BASIC Field Statement
  2.  
  3.             Bill Consiglio
  4.      Boca Raton IBM PC Users Group
  5.  
  6. The FIELD statement is used to
  7. allocate space for variables in a
  8. random file buffer. Having seen a few
  9. of the mistakes programmers have made
  10. with the FIELD statement and having
  11. answered some of their questions, I
  12. submit the following comments and
  13. hints on the subject.
  14.  
  15. The most common mistake I've seen
  16. involves neglecting to use arrays in
  17. the FIELD statement. All the examples
  18. in the manual show discrete variables
  19. named in the FIELD, whereas in many
  20. programs the advantages of array
  21. processing can greatly simplify the
  22. design and code. The FIELD statement:
  23.  
  24. FIELD #1,8 AS R1$(1),4 AS R1$(2),4 AS
  25.   R1$(3)
  26.  
  27. can be processed by the statement:
  28.  
  29. FOR J=1 TO 3: LSET R1$(J)=R$(J):
  30.   NEXT J
  31.  
  32. This simplistic example becomes more
  33. meaningful as the process within the
  34. loop gets larger and would have to be
  35. re-coded for each unique variable in
  36. the FIELD statement. Many programs,
  37. for instance, fill in the buffer
  38. variables by prompting the user with
  39. input requests. This can be done
  40. nicely in a loop.
  41.  
  42. The following example illustrates
  43. several points:
  44.  
  45. 10 OPEN "TEST" AS #1 LEN=100
  46. 20 '   name   address   city
  47. 30 FIELD #1,20 AS R1$(1),20 AS
  48.    R1$(2), 15 AS R1#(3)
  49. 40 FOR J=1 TO 3: LN=LN+LEN(R1$(J)):
  50.    NEXT J
  51. 50 '        state    zip    phone
  52. 60 FIELD #1,LN AS DUMMY$,2 AS R1$(4),
  53.    5 AS R1$(5),8 AS R1$(6)
  54. (etc.)
  55.  
  56. The comment lines (20,50) serve as
  57. reminders about the meaning of each
  58. field variable.
  59.  
  60. FIELD statements, by their very
  61. nature, tend to get quite long. They
  62. can be kept to a manageable length by
  63. breaking them up into several FIELD
  64. statements.  The trick to this is
  65. including the total length of all the
  66. preceding fields in a "dummy"
  67. variable at the start of the next
  68. continued FIELD (as in line 60). The
  69. length of the "dummy" field can be
  70. inserted manually during coding or,
  71. as in the example, it can be
  72. calculated at execution time (line
  73. 40). This procedure can be repeated
  74. for as many FIELD statements as are
  75. required.
  76.  
  77. The next example does the same field
  78. definitions as the previous one. It
  79. uses a DATA statement (line 40) to
  80. define the prompt and length for each
  81. field.
  82.  
  83. 10 OPEN "TEST" AS #1 LEN=100
  84. 15 FOR J=1 TO 6
  85. 20   READ PROMPT$(J),FLDLEN
  86. 25   FIELD #1,LN AS DUMMY$,FLDLEN AS
  87.      R1$(J)
  88. 30   LN=LN+FLDLEN
  89. 35 NEXT J
  90. 40 DATA Name,20,Address,20,City,15,
  91.    State,2,Zip,5,Phone,8
  92. 45 CLS
  93. 50 FOR J=1 TO 6
  94. 55   PRINT PROMPT$(J);TAB(15);" - [";
  95.      SPACE$(LEN(R1$(J)));""";
  96. 60   LOCATE ,19,1: LINE INPUT "",Z$
  97. 65   LSET R1$(J)=Z$
  98. 70 NEXT J
  99.  
  100.  
  101. (etc.)
  102.  
  103. In addition, line 55 makes use of the
  104. length of each field to show the user
  105. how many characters can be typed in
  106. the field. For example, the first
  107. prompt appears on the screen as:
  108.  
  109. Name         -[_             ]
  110.  
  111. In this simple program, the brackets
  112. do not enforce the string length,
  113. that is, a user can type something
  114. longer, and a backspace will cause
  115. the ] to move left, but both of these
  116. minor problems can be fixed with a
  117. little more code.
  118.