DATA: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The DATA statement creates a line of fixed program information separated by commas. The DATA can be later READ by the program at runtime. {{PageSyntax}} : DATA [value1, value2, ...] {{PageDescription}} * DATA is used at the beginning of every data field line with commas separating the values that follow. * Values can be any '''literal''' STRING or numerical type. '''Variables cannot be used.''' * DATA fields can be placed and READ consecutively in the mai...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
* DATA is used at the beginning of every data field line with commas separating the values that follow.
* DATA is used at the beginning of every data field line with commas separating the values that follow.
* Values can be any '''literal''' [[STRING]] or numerical type. '''Variables cannot be used.'''
* Values can be any '''literal''' [[STRING]] or numerical type. '''Variables cannot be used.'''
* DATA fields can be placed and READ consecutively in the main program code body with or without line labels for [[RESTORE]].  
* DATA fields can be placed and READ consecutively in the main program code body with or without line labels for [[RESTORE]].
* DATA is best placed after the main program code.
* DATA is best placed after the main program code.
** '''QB64''' DATA can be placed inside a [[SUB]] or  [[FUNCTION]] procedures.
** '''QB64''' DATA can be placed inside a [[SUB]] or  [[FUNCTION]] procedures.
Line 24: Line 24:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Creating two DATA fields that can be [[READ]] repeatedly using [[RESTORE]] with the appropriate line label.
''Example 1:'' Creating two DATA fields that can be [[READ]] repeatedly using [[RESTORE]] with the appropriate line label.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|RESTORE}} Database2
{{Cl|RESTORE}} Database2
{{Cl|READ}} A$, B$, C$, D$        'read 4 string values from second DATA field
{{Cl|READ}} A$, B$, C$, D$        'read 4 string values from second DATA field
Line 32: Line 32:
FOR i = 1 TO 18
FOR i = 1 TO 18
   {{Cl|READ}} number%                    'read first DATA field 18 times only
   {{Cl|READ}} number%                    'read first DATA field 18 times only
   PRINT number%;                  
   PRINT number%;
NEXT
NEXT


Line 42: Line 42:


Database2:
Database2:
{{Cl|DATA}} "Hello, ", "world! ", Goodbye, work! '' ''
{{Cl|DATA}} "Hello, ", "world! ", Goodbye, work!
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}Hello world! Goodbyework!
{{OutputStart}}Hello world! Goodbyework!
Line 50: Line 50:


''Example 2:'' How to [[RESTORE]] and [[READ]] DATA in a [[SUB]] procedure in QB64. Line labels can be used for multiple DATA fields.
''Example 2:'' How to [[RESTORE]] and [[READ]] DATA in a [[SUB]] procedure in QB64. Line labels can be used for multiple DATA fields.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DIM}} {{Cl|SHARED}} num(10) 'shared array or must be passed as a parameter
{{Cl|DIM}} {{Cl|SHARED}} num(10) 'shared array or must be passed as a parameter
ReadData 2 '<<<<<<< change value to 1 to read other data
ReadData 2 '<<<<<<< change value to 1 to read other data
Line 68: Line 68:
mydata2:
mydata2:
{{Cl|DATA}} 10,9,8,7,6,5,4,3,2,1
{{Cl|DATA}} 10,9,8,7,6,5,4,3,2,1
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}} 10  9  8  7  6  5  4  3  2  1 {{OutputEnd}}
{{OutputStart}} 10  9  8  7  6  5  4  3  2  1 {{OutputEnd}}




{{PageSeeAlso}}  
{{PageSeeAlso}}
* [[READ]]  
* [[RESTORE]]. [[READ]]
* [[RESTORE]]
* [[$EMBED]]. [[_EMBEDDED$]]
* [[SUB]], [[FUNCTION]]
* [[SUB]], [[FUNCTION]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 17:51, 16 January 2024

The DATA statement creates a line of fixed program information separated by commas. The DATA can be later READ by the program at runtime.


Syntax

DATA [value1, value2, ...]


Description

  • DATA is used at the beginning of every data field line with commas separating the values that follow.
  • Values can be any literal STRING or numerical type. Variables cannot be used.
  • DATA fields can be placed and READ consecutively in the main program code body with or without line labels for RESTORE.
  • DATA is best placed after the main program code.
    • QB64 DATA can be placed inside a SUB or FUNCTION procedures.
  • RESTORE will only read the first data field if the DATA is not labeled or no label is specified in a RESTORE call.
  • When using multiple DATA fields, label each data field with a line label so that each data pointer can be reset for multiple reads with RESTORE linelabel.
  • QBasic comma separations were flexible to allow column alignments when creating them. QB64 removes spacing between commas.
  • STRING DATA values with end spaces, QBasic keywords and values that include the comma character must be enclosed in quotation marks.
  • DATA fields can only be created by the programmer and cannot be changed by a user or lost.
  • Comments after a data line require a colon before the comment.
  • If a READ statement attempts to read past the last data value, an "Out of Data" error will occur. Use end of data markers when necessary.
  • DATA fields can be placed after SUB or FUNCTION procedures, but line labels are not allowed.


Examples

Example 1: Creating two DATA fields that can be READ repeatedly using RESTORE with the appropriate line label.

RESTORE Database2
READ A$, B$, C$, D$         'read 4 string values from second DATA field
PRINT A$ + B$ + C$ + D$     'note that quoted strings values are spaced

RESTORE Database1
FOR i = 1 TO 18
  READ number%                     'read first DATA field 18 times only
  PRINT number%;
NEXT

END

Database1:
DATA 1, 0, 0, 1, 1, 0, 1, 1, 1
DATA 2, 0, 0, 2, 2, 0, 2, 2, 2 :       ' DATA line comments require a colon

Database2:
DATA "Hello, ", "world! ", Goodbye, work!
Hello world! Goodbyework!
1  0  0  1  1  0  1  1  1  2  0  0  2  2  0  2  2  2


Example 2: How to RESTORE and READ DATA in a SUB procedure in QB64. Line labels can be used for multiple DATA fields.

DIM SHARED num(10) 'shared array or must be passed as a parameter
ReadData 2 '<<<<<<< change value to 1 to read other data
FOR i = 1 TO 10
  PRINT num(i);
NEXT
END

SUB ReadData (mode)
IF mode = 1 THEN RESTORE mydata1 ELSE RESTORE mydata2
FOR i = 1 TO 10
  READ num(i)
NEXT

mydata1:
DATA 1,2,3,4,5,6,7,8,9,10
mydata2:
DATA 10,9,8,7,6,5,4,3,2,1
END SUB
 10  9  8  7  6  5  4  3  2  1 


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage