![]() |
Forum Index : Microcontroller and PC projects : Can't read multiple variables with INPUT #
Author | Message | ||||
PeteCotton![]() Guru ![]() Joined: 13/08/2020 Location: CanadaPosts: 529 |
I'm being a bit thick - and this is almost certainly operator error, but I was trying to write out multiple integer values to a file and read them back in. If I'm reading the manual right, I should be able to do INPUT #1,a,b to read values into a and b![]() But when I run the code below, it writes the variables out okay to the file. But when I do the reading back in, it loads a but not b ' This bit works - the file contains 1,2 DIM INTEGER a=1, b=2 OPEN "test.dat" FOR OUTPUT AS #1 PRINT #1,a,b CLOSE #1 a=0 b=0 ' This is where I get confused OPEN "test.dat" FOR INPUT AS #1 INPUT #1,a,b CLOSE #1 PRINT a PRINT b Results in an output of a=1 and b=0 What am I doing wrong? I should note - that this is just for my own curiosity, I saved the data one variable at a time to each newline and that worked absolutely fine - I'm just trying to understand how to do it for the future. Thanks. |
||||
PeteCotton![]() Guru ![]() Joined: 13/08/2020 Location: CanadaPosts: 529 |
Well, don't I feel a bit foolish. I re-read an old BASIC page and they used this line: PRINT #1,a;",";b to write out the data. An I just tested it and it worked fine.Sorry for the stupid question (it is quite late here - that's my excuse and I'm sticking to it ![]() |
||||
Peter63 Regular Member ![]() Joined: 28/07/2017 Location: SwedenPosts: 47 |
You found a solution to this yourself ![]() /Peter63 |
||||
PeteCotton![]() Guru ![]() Joined: 13/08/2020 Location: CanadaPosts: 529 |
Yes. PRINT #1,a;",";b Stored it as 1, 2 PRINT #1,a,b Stored it as 1 2 Cheers, Pete |
||||
Peter63 Regular Member ![]() Joined: 28/07/2017 Location: SwedenPosts: 47 |
OK, thank you ![]() /Peter63 |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2458 |
There was a recent demo. of that in formatting csv files thread. |
||||
PeteCotton![]() Guru ![]() Joined: 13/08/2020 Location: CanadaPosts: 529 |
Doh! I read that thread as well, but it just didn't register with me ![]() |
||||
PeteCotton![]() Guru ![]() Joined: 13/08/2020 Location: CanadaPosts: 529 |
Yes, I think my problem is that I misremember things about BASIC, and then expect things to work one way - and when they don't, I get confused (e.g. my other thread thinking that BASIC arrays stared at Base 1). It's about retraining my brain. I had a problem the other day which tripped me up - which was I was using "!=" instead of "<>" for not equals. But my brain reads both as "not equals", so I couldn't see what was wrong with the syntax. ![]() Still - even working through these sorts of issues is a lot of fun. There's that sense of discovery I had back in my youth. Modern languages do everything so easily and so quickly, that it's rare that you have to optimise anything. With BASIC it feels a lot more "old school". Case in point. I'm going to take my new found knowledge about how to save multiple variables at a time, and see if it shaves some millseconds off the save/load speed of my game. |
||||
toml_12953 Guru ![]() Joined: 13/02/2015 Location: United StatesPosts: 396 |
Some BASICs do start arrays at one. In fact the ANSI/ISO standard for BASIC is one of those. It supplies the OPTION BASE command for changing to 0 based indexing but it really isn't needed since you can define the dimensions in a DIM statement: DIM A(0 TO 5) will give you 6 elements no matter what OPTION BASE is set to DIM A(101 TO 110) starts at 101 and provides 10 elements (of course!) This makes it great for keeping track of yearly figures: DIM PROFIT(1947 TO 2025) can store info on years 1947 to 2025 and if you want the profit for the year 1963, for example, you can just retrieve PROFIT(1963) instead of having to figure out which element stores that. Edited 2025-02-17 07:49 by toml_12953 |
||||
PeteCotton![]() Guru ![]() Joined: 13/08/2020 Location: CanadaPosts: 529 |
I didn't know that. Very interesting. Thanks! |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2458 |
Not as convenient as DIM PROFIT(1947 TO 2025) but but not too bad. Use element 0 to store the offset and treat the rest of the array as if it were base 1. > Dim PROFIT(2025 - 1947 + 1) > PROFIT(0) = 1947 - 1 > ? PROFIT(0) 1946 > for n=1 to (2025 - 1947 + 1) :PROFIT(n)=n+100 :next > ? PROFIT(1947-PROFIT(0)) 101 > ? PROFIT(2024-PROFIT(0)) 178 > ? PROFIT(2025-PROFIT(0)) 179 > |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |