Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Must an extra value be provided on DATA statement?
#1
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: 
  1. Start the DATA with a count of how many to read, not including the count itself.
  2. Read at most that many. 
  3. Always add an extra value which will not be read.
Reply
#2
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, …]

Blush
Reply
#3
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
Reply
#4
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!
Reply
#5
(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, …]

Blush

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.
Reply
#6
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! Smile

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
Reply
#7
RTFM never goes out of style.
Thank you all for the replies and comments.
Reply
#8
(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! Smile

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.  Big Grin  Tongue  Big Grin
Reply
#9
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 Smile
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#10
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."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking Matches in a Data Base Dimster 10 1,028 07-13-2025, 12:26 AM
Last Post: Dimster
  _Glue statement coming anytime soon? Pete 7 1,288 04-13-2025, 10:56 PM
Last Post: eoredson
  Change file data krovit 5 1,186 07-11-2024, 07:45 AM
Last Post: krovit
  The Width statement eoredson 15 2,260 04-06-2024, 03:57 AM
Last Post: Pete
  Arrays In User Data Types Consolemu 2 859 01-18-2024, 09:49 PM
Last Post: bplus

Forum Jump:


Users browsing this thread: 1 Guest(s)