Hi everyone,
I understand the use of READ and DATA but am puzzled as to how READ knows which DATA to read?
I've seen listings of programs in books and some have multiple READ and DATA statements, none of which have any identifying labels to allow some form of referencing.
How does READ know which set of DATA to read in?
Thanks.
John
READ and DATA
- hellomike
- Posts: 192
- Joined: Sat 09 Jun 2018, 09:47
- Location: Amsterdam
Re: READ and DATA
Hi John,
As far as I understand, READ will start reading from the current "data pointer". This pointer can be manipulated using the RESTORE keyword.
In the help for this keyword, it reads "A statement which moves the data pointer. RESTORE can be used at any time in a program to set the line from where READ reads the next DATA item."
The help that comes with this programming language is as complete and powerful as the language itself!
I cannot emphasize enough to consult the help information as I can almost guarantee that you always find your answers in there.
Its not that I don't want to help as all the other people in this forum but I really think that you find the info quicker from the help documentation than waiting for an answer in the forum.
Hope this helps.
Mike
As far as I understand, READ will start reading from the current "data pointer". This pointer can be manipulated using the RESTORE keyword.
In the help for this keyword, it reads "A statement which moves the data pointer. RESTORE can be used at any time in a program to set the line from where READ reads the next DATA item."
The help that comes with this programming language is as complete and powerful as the language itself!
I cannot emphasize enough to consult the help information as I can almost guarantee that you always find your answers in there.
Its not that I don't want to help as all the other people in this forum but I really think that you find the info quicker from the help documentation than waiting for an answer in the forum.
Hope this helps.
Mike
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: READ and DATA
Thank you Mike for your kind response. I am sorry to be a pain.
I always do read the help first and only post when the answer is not clear or I cannot find the help.
I still don't know how READ finds which DATA set it should use.
Let's say I have a long program with five lines scattered in the listing with a READ statement (not all together, but throughout the program listing).
Then, at the bottom of the program listing, are 100's of lines with DATA.
My question (which isn't in any help that I can find) is, how does each READ statement know which DATA line to go to when there isn't just one set?
Thanks again.
John
I always do read the help first and only post when the answer is not clear or I cannot find the help.
I still don't know how READ finds which DATA set it should use.
Let's say I have a long program with five lines scattered in the listing with a READ statement (not all together, but throughout the program listing).
Then, at the bottom of the program listing, are 100's of lines with DATA.
My question (which isn't in any help that I can find) is, how does each READ statement know which DATA line to go to when there isn't just one set?
Thanks again.
John
- hellomike
- Posts: 192
- Joined: Sat 09 Jun 2018, 09:47
- Location: Amsterdam
Re: READ and DATA
Nah, no pain at all John. Happy to help.
Well, I think this "data pointer" the documentation mentions is at the top of the program when it start running. The 1ste READ will make it start searching for the 1st DATA line in the source. So it makes no difference if that DATA statement is at the first line or the last line of a 72634 line program.
Consider the following example:
It is pretty straight forward and 'readable'. I mean it's convenient that the data is near the code that uses it.
The DATA lines however can be anywhere in the program! Both at the top, at the bottom, scattered through the source (i.e. 12 + 7 separate DATA lines with one data item each), etc. It doesn't matter because BB4W will start looking from the top once you start running the program.
Now I change the source having all DATA statements at the bottom (and in wrong order). It doesn't error out but it doesn't print what you want because the first READ will get the data from the first DATA statement it encounters (from the entire source).
I.e. the data pointer is global, so NOT relative to the READ statement. You cannot have multiple data pointers, however you CAN make the data pointer start from a specific point using the RESTORE statement.
Actually you CAN have multiple pointers, when using LOCAL DATA but try to grasp this first.
So, READ does NOT know what DATA it is suppose to read. It just reads from the 1st DATA statement the data pointer encounters (top to bottom).
Well, I think this "data pointer" the documentation mentions is at the top of the program when it start running. The 1ste READ will make it start searching for the 1st DATA line in the source. So it makes no difference if that DATA statement is at the first line or the last line of a 72634 line program.
Consider the following example:
Code: Select all
10 DATA Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
20 PRINT "Months are:"
30 FOR I%=1 TO 12
40 READ Month$
50 PRINT Month$
60 NEXT
70
80 DATA Sun, Mon, Tue, Wed, Thu, Fri, Sat
90 PRINT '"Days are:"
100 FOR I%=1 TO 7
110 READ Day$
120 PRINT Day$
130 NEXT
140 END
The DATA lines however can be anywhere in the program! Both at the top, at the bottom, scattered through the source (i.e. 12 + 7 separate DATA lines with one data item each), etc. It doesn't matter because BB4W will start looking from the top once you start running the program.
Now I change the source having all DATA statements at the bottom (and in wrong order). It doesn't error out but it doesn't print what you want because the first READ will get the data from the first DATA statement it encounters (from the entire source).
Code: Select all
10 PRINT "Months are:"
20 FOR I%=1 TO 12
30 READ Month$
40 PRINT Month$
50 NEXT
60
70 PRINT '"Days are:"
80 FOR I%=1 TO 7
90 READ Day$
100 PRINT Day$
110 NEXT
120 END
130
140 DATA Sun, Mon, Tue, Wed, Thu, Fri, Sat
150 DATA Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
Actually you CAN have multiple pointers, when using LOCAL DATA but try to grasp this first.
So, READ does NOT know what DATA it is suppose to read. It just reads from the 1st DATA statement the data pointer encounters (top to bottom).
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: READ and DATA
Thank you Mike.
That's a great explanation. Could that not be added to the user guide please Richard?
Cheers Mike. Really appreciate your help (once again)
John
That's a great explanation. Could that not be added to the user guide please Richard?
Cheers Mike. Really appreciate your help (once again)
John
Re: READ and DATA
Hi John,
Just to add, you don't need line numbers, you can use labels, which may be clearer (and less at risk if you change the program structure later).
Here's an example
Best wishes,
D
Just to add, you don't need line numbers, you can use labels, which may be clearer (and less at risk if you change the program structure later).
Here's an example
Best wishes,
D
Code: Select all
DIM months$(11),days$(6)
RESTORE (month_data)
FOR x%=0 TO 11
READ months$(x%)
NEXT x%
RESTORE (day_data)
FOR x%=0 TO 6
READ days$(x%)
NEXT x%
(irrelevant_data)
DATA 1,2,3,4,5,"garbage"
(day_data)
DATA "Monday", Tuesday, Wednesday,Thursday,Friday,Saturday,Sunday
(month_data)
DATA Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
FOR x%=0 TO 6
PRINT days$(x%);" ";
NEXT x%
PRINT
FOR x%=0 TO 11
PRINT months$(x%);" ";
NEXT x%
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: READ and DATA
Thank you DDRM.
Appreciate your further help.
Thank you everyone.
Appreciate your further help.
Thank you everyone.