Basic Data

Blitz3D Forums/Blitz3D Beginners Area/Basic Data

Kirkkaf13(Posted 2010) [#1]
Hi,

I was just reading about data in the Blitz3D manual.

In the example they restore .startData

When it comes to reading data they read Uses, which reads the top line, so from resoring the data label, every time you read it reads a new line?

But in the FOR loop they read data multiple times, How does it know which bit you are reading with this?




Hope this makes sense,

- Kirk.


Matty(Posted 2010) [#2]
Because each 'read' statement doesn't read a single line but instead reads a single 'data' entry, sequentially (separated by commas).


Kirkkaf13(Posted 2010) [#3]
Hi Matt thanks for your reply. If I was to have this data:



How would I just read for example the Age being 100?

Would I have to first read the FirstName$ & SecondName$?

With the last basic language I used when you read data you wrote:



- Thank you,

Kirk.


Matty(Posted 2010) [#4]
I think (although I've not used data statements much at all) you could do it as you said OR as the read statement simply reads the next available data element in the set of data you could read the data in 1 element at a time with separate read statements.

My own personal preference is to use an external file to store data in, but the data statement does have its place too.


Kirkkaf13(Posted 2010) [#5]
Cheers Matty,

I have gone throught the basic manual of Blitz3D and I understand how everything works there, where do I go from here?

- Kirk.


PowerPC603(Posted 2010) [#6]
When you use the Restore command, the program points to the label you've given ("startData" in your first example).

Then each time when you use the Read command, it points to the next value in the data-statements, reads the value and stores it in the variable you've provided (Read firstname$ for example, reads the next value and stores it in firstname$).

If you were to use the Restore command in the loop, you would only read the top values of the data-statements.
; Restore to the start of the Data statements
Restore startData

; Get the first number which is the total number of users
Read Users

; Print them all!
For T = 1 To Users
Restore startData

Read firstname$
Read age
Read accuracy#
Read lastname$
Print firstname$ + " " + lastname$ + " is " + age + " years old with " + accuracy#  + " accuracy!"
Next 

.startData
Data 3
Data "Shane", 31, 33.3333, "Monroe"
Data "Bob", 28, 12.25, "Smith"
Data "Roger", 54, 66.66, "Rabbit"


If you would use this code, then it would read the first 2 lines (3, "Shane", 31, 33.3333) 3 times, as the Restore command jumps to the first data-entry following the label .startData every loop.

As you've noticed, the variables are now wrong, as you would expect to read the firstname first, but now it's the value 3 you've read and stored in firstname$. It's the first data-entry following the .startData label.
For the age, you would read "Shane", which would be converted to a 0 (zero), as you can't store strings in an int variable.
For the accuracy#, you would read 31 instead of 33.3333.
And lastly, lastname$ would contain "33.3333" as a string.

As the manual also says, you can use multiple variables to read:
Read firstname$, age, accuracy#, lastname$



Kirkkaf13(Posted 2010) [#7]
Thanks for this explaination PowerPC603. This make me understand things alot better.

- Kirk.