READ: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "The '''READ''' statement reads values from a DATA field and assigns them to one or a comma separated list of variables. {{PageSyntax}} ::: READ value1$[, value2!, value3%, ...] * READ statements assign variables to DATA statement values on a one-to-one basis sequentially. * A single READ statement may access one or more DATA values. They are accessed in the order set. * Several READ statements may access the same DATA statement block at the foll...") |
No edit summary |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
* READ statements assign variables to [[DATA]] statement values on a one-to-one basis sequentially. | * READ statements assign variables to [[DATA]] statement values on a one-to-one basis sequentially. | ||
* A single READ statement may access one or more [[DATA]] values. They are accessed in the order set. | * A single READ statement may access one or more [[DATA]] values. They are accessed in the order set. | ||
* Several READ statements may access the same [[DATA]] statement block at the following sequential position. | * Several READ statements may access the same [[DATA]] statement block at the following sequential position. | ||
* [[DATA]] can be READ using [[STRING]] or numerical [[TYPE]] variables singularly or in a comma separated list: | * [[DATA]] can be READ using [[STRING]] or numerical [[TYPE]] variables singularly or in a comma separated list: | ||
:: [[STRING]] READ variables can read quoted or unquoted text or numerical DATA values! | :: [[STRING]] READ variables can read quoted or unquoted text or numerical DATA values! | ||
:: Numerical type READ variables can only read '''unquoted''' numerical DATA values! | :: Numerical type READ variables can only read '''unquoted''' numerical DATA values! | ||
::'''If they do not agree, a [[ERROR Codes|"Syntax error"]] may result when run reading string data as numerical values!''' | ::'''If they do not agree, a [[ERROR Codes|"Syntax error"]] may result when run reading string data as numerical values!''' | ||
* If the number of variables specified is fewer than the number of elements in the DATA statement(s), subsequent READ statements begin reading data at the next unread element. If there are no subsequent READ statements, the extra data is ignored. | * If the number of variables specified is fewer than the number of elements in the DATA statement(s), subsequent READ statements begin reading data at the next unread element. If there are no subsequent READ statements, the extra data is ignored. | ||
Line 18: | Line 18: | ||
* [[ACCESS]] READ can be used in an [[OPEN]] statement to limit file access to read only, preserving file data. | * [[ACCESS]] READ can be used in an [[OPEN]] statement to limit file access to read only, preserving file data. | ||
* '''WARNING! Do not place DATA fields after [[SUB]] or [[FUNCTION]] procedures! QB64 will FAIL to compile properly!''' | * '''WARNING! Do not place DATA fields after [[SUB]] or [[FUNCTION]] procedures! QB64 will FAIL to compile properly!''' | ||
: | : QBasic allowed programmers to add DATA fields anywhere because the IDE separated the main code from other procedures. | ||
Line 33: | Line 33: | ||
{{Cl|DATA}} 3.08, 5.19, 3.12, 3.98, 4.24 | {{Cl|DATA}} 3.08, 5.19, 3.12, 3.98, 4.24 | ||
{{Cl|DATA}} 5.08, 5.55, 4.00, 3.16, 3.37 | {{Cl|DATA}} 5.08, 5.55, 4.00, 3.16, 3.37 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Line 48: | Line 48: | ||
PRINT C$, S$, Z& | PRINT C$, S$, Z& | ||
{{Cl|DATA}} "DENVER,", COLORADO, 80211 | {{Cl|DATA}} "DENVER,", COLORADO, 80211 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Line 55: | Line 55: | ||
DENVER, COLORADO 80211 | DENVER, COLORADO 80211 | ||
{{OutputEnd}} | {{OutputEnd}} | ||
:''Note:'' String DATA values do not require quotes unless they contain commas, end spaces or | :''Note:'' String DATA values do not require quotes unless they contain commas, end spaces or QBasic keywords. | ||
{{PageSeeAlso}} | |||
* [[DATA]], [[RESTORE]] | * [[DATA]], [[RESTORE]] | ||
* [[ | * [[$EMBED]]. [[_EMBEDDED$]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 17:55, 16 January 2024
The READ statement reads values from a DATA field and assigns them to one or a comma separated list of variables.
Syntax
- READ value1$[, value2!, value3%, ...]
- READ statements assign variables to DATA statement values on a one-to-one basis sequentially.
- A single READ statement may access one or more DATA values. They are accessed in the order set.
- Several READ statements may access the same DATA statement block at the following sequential position.
- DATA can be READ using STRING or numerical TYPE variables singularly or in a comma separated list:
- STRING READ variables can read quoted or unquoted text or numerical DATA values!
- Numerical type READ variables can only read unquoted numerical DATA values!
- If they do not agree, a "Syntax error" may result when run reading string data as numerical values!
- If the number of variables specified is fewer than the number of elements in the DATA statement(s), subsequent READ statements begin reading data at the next unread element. If there are no subsequent READ statements, the extra data is ignored.
- If variable reads exceed the number of elements in the DATA field(s), an "Out of data" error will occur!
- Use the RESTORE statement to reread DATA statements from the start, with or without a line label as required.
- ACCESS READ can be used in an OPEN statement to limit file access to read only, preserving file data.
- WARNING! Do not place DATA fields after SUB or FUNCTION procedures! QB64 will FAIL to compile properly!
- QBasic allowed programmers to add DATA fields anywhere because the IDE separated the main code from other procedures.
Example 1: Placing data into an array.
DIM A(10) AS SINGLE FOR I = 1 TO 10 READ A(I) NEXT I FOR J = 1 TO 10 PRINT A(J); NEXT END DATA 3.08, 5.19, 3.12, 3.98, 4.24 DATA 5.08, 5.55, 4.00, 3.16, 3.37 |
3.08 5.19 3.12 3.98 4.24 5.08 5.55 4 3.16 3.37 |
- Explanation: This program reads the values from the DATA statements into array A. After execution, the value of A(1) is 3.08, and so on. The DATA statements may be placed anywhere in the program; they may even be placed ahead of the READ statement.
Example 2: Reading three pieces of data at once.
PRINT " CITY ", " STATE ", " ZIP" PRINT STRING$(30, "-") 'divider READ C$, S$, Z& PRINT C$, S$, Z& DATA "DENVER,", COLORADO, 80211 |
CITY STATE ZIP ------------------------------ DENVER, COLORADO 80211 |
- Note: String DATA values do not require quotes unless they contain commas, end spaces or QBasic keywords.