Posts: 7
Threads: 2
Joined: Sep 2025
Reputation:
0
When my program reads the last item in a DATA statement, the program experiences an error:
Code: (Select All) Line 12: (in main module) Syntax error
Is this expected?
Example:
Code: (Select All)
' Demonstrate defect in DATA handling
mydata: Data 8,1,2,3,4,5,6,7,8,9999 ' start with the quantity of items to read,end with an extra
Restore mydata
Read datalen ' get the length
Print datalen
For i = 1 To datalen
Read j
Print j,
Next
Print
Read extraval
Print extraval
' Line 12: (in main module) Syntax error
Problem resolution:
- Start the DATA with a count of how many to read, not including the count itself.
- Read at most that many.
- Always add an extra value which will not be read.
Posts: 7
Threads: 2
Joined: Sep 2025
Reputation:
0
09-29-2025, 01:42 PM
(This post was last modified: 09-29-2025, 01:45 PM by dakra137.)
No, it is not necessary to add a superfluous value to the DATA statement.
It is sufficient to add a not-really-superfluous comma after the last item to be read.
'instead of
'mydata: Data 8,1,2,3,4,5,6,7,8,9999
'use:
mydata: Data 8,1,2,3,4,5,6,7,8,9999 ,
The QB64PE documentation page syntax diagram for the DATA statement clearly shows that every value is followed by a comma:
DATA [value1, value2 , …]
Posts: 4,695
Threads: 222
Joined: Apr 2022
Reputation:
322
Wow that was not expected by me. I would never guess DATA needed to end in comma. Been a long time since I used a DATA statement but I don't remember it needed to end in comma. When did that start I wonder?
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
Posts: 3,446
Threads: 376
Joined: Apr 2022
Reputation:
345
09-29-2025, 02:44 PM
(This post was last modified: 09-29-2025, 02:51 PM by SMcNeill.)
That's not the issue at all. I've went over this several times and folks can do a search and find the issue talked about repeatedly in the past.
The issue is YOU'RE TRYING TO READ A STRING INTO A NUMBER!!!
Try this very simple code:
Code: (Select All)
Read a$
Print a$
Data 'Steves Awesome!!
Now, is my Data just blank, and then Steve's Awesome is a comment??
Run it and find out.
Spoiler alert -- GASP! It's all one string! /faint.
So what you have is:
Data 8,1,2,3,4,5,6,7,8,9999 ' start with the quantity of items to read,end with an extra
8
1
2
3
4
5
6
7
8
"9999 ' start with the quantity of items to read,end with an extra"
And gosh, what do you know, when you try to read that STRING into a NUMERIC variable, that somehow generates an error.
Simple fixes:
1) Move that comment off that line.
2) Use colons as an end of DATA seperator:
Data 8,1,2,3,4,5,6,7,8,9999 : ' start with the quantity of items to read,end with an extra
Presto!! Issue solved.
Read strings as strings. Read numbers as numbers. And remember -- the remark symbol IS still a valid character for string data inside a DATA statement. It's not a comment!
Posts: 3,446
Threads: 376
Joined: Apr 2022
Reputation:
345
(09-29-2025, 01:42 PM)dakra137 Wrote: No, it is not necessary to add a superfluous value to the DATA statement.
It is sufficient to add a not-really-superfluous comma after the last item to be read.
'instead of
'mydata: Data 8,1,2,3,4,5,6,7,8,9999
'use:
mydata: Data 8,1,2,3,4,5,6,7,8,9999,
The QB64PE documentation page syntax diagram for the DATA statement clearly shows that every value is followed by a comma:
DATA [value1, value2, …]

Kindly look at the samples for the wiki page.
And then remember to look at this highlight from the description:
Comments after a data line require a colon before the comment.
Also notice the example on the wiki page:
Code: (Select All)
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!
The issue isn't any need to end DATA with a comma. That's not required at all.
The issue is reading a STRING as a NUMBER and blipping an error attempting it.
Posts: 4,695
Threads: 222
Joined: Apr 2022
Reputation:
322
Oh so it was the comment not done properly that ruined the expected run of the program.
This works fine!
Code: (Select All) ' Demonstrate defect in DATA handling
mydata: Data 8,1,2,3,4,5,6,7,8,9999
' start with the quantity of items to read,end with an extra
Restore mydata
Read datalen ' get the length
Print datalen
For i = 1 To datalen
Read j
Print j,
Next
Print
Read extraval
Print extraval
' Line 12: (in main module) Syntax error
Steve was too wordy talking at first about strings and such, so I cut to the chase!
So my memory still serves me, no extra comma needed at the end of DATA statement. ha
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
Posts: 7
Threads: 2
Joined: Sep 2025
Reputation:
0
RTFM never goes out of style.
Thank you all for the replies and comments.
Posts: 3,446
Threads: 376
Joined: Apr 2022
Reputation:
345
(09-29-2025, 05:39 PM)bplus Wrote: Oh so it was the comment not done properly that ruined the expected run of the program.
Steve was too wordy talking at first about strings and such, so I cut to the chase! 
So my memory still serves me, no extra comma needed at the end of DATA statement. ha
bplus gets a dminus for reading comprehension. LOL!! You must've just skimmed the first post? Go back and look once again.
How'd you miss the most important part?
Quote:Simple fixes:
1) Move that comment off that line.
2) Use colons as an end of DATA separator:
Data 8,1,2,3,4,5,6,7,8,9999 : ' start with the quantity of items to read,end with an extra
Presto!! Issue solved.
Steve was sooo wordy, bplus decided he needed to write an entire demo to showcase the solution Steve mentioned.
Posts: 4,695
Threads: 222
Joined: Apr 2022
Reputation:
322
bplus always gets a dminus for reading comprehension when he see walls of words, bplus gets an f in patience too, nobody should mistake him for a saint LOL!
Why 2 posts when one corrected code post will do? because Steve is a writer while bplus would rather do something else in time it takes to read 2 posts on same subject.
Of course we have to give bplus a bplus in defensiveness
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever
Posts: 188
Threads: 14
Joined: May 2024
Reputation:
20
i'm not sure if anybody mentioned it here yet.
from https://qb64phoenix.com/qb64wiki/index.php?title=DATA
Quote:* DATA is best placed after the main program code.
* QB64 DATA can be placed inside a SUB or FUNCTION procedures.
* DATA fields can be placed after SUB or FUNCTION procedures, but line labels are not allowed.
but if that program is going to get any larger. my advice is that any data lines are placed at the end of module level code. after any gosub subroutine. after any on error goto execution section which includes resume. before any sub or function definition. delimit data sets if you need to with labels. but putting data line as one of the first things in the program. is confusing for some people. who read qb64 code. and expect it "in the middle."
|