Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : How to indirectly reference variables’ values
Author | Message | ||||
carlschneider Senior Member Joined: 04/08/2023 Location: South AfricaPosts: 158 |
I’m hoping somebody has an elegant solution. I have the following variables a=10 b=19 . . . z=1 which have values assigned to them as indicated. I want to use a loop, something along these lines below, to print the values of a, b through z. For i=97 to 122 Print chr$(i) Next I Where the output would be 10 19 . . . 1 Effectively I’m trying to avoid an array. I was hoping that I could use EXECUTE command$ to produce the value assigned to the variable a, but haven’t succeeded. Any guidance would be appreciated. Cheers Carl Retirement is tough on Hobbies without a day job |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9110 |
a=20 b=11 c=12 d=13 For i=Asc("a") To Asc("d") Print Eval(Chr$(i)) Next |
||||
carlschneider Senior Member Joined: 04/08/2023 Location: South AfricaPosts: 158 |
Hi Peter Thank you very much, I missed EVAL in trying to find something in the manual. Is there a way of doing an INPUT #1 from a file into a Longstring? So far I’ve only been able to use a normal 255 character string as the destination. Looking for the same longstring destination capability as the WEB interrupt commands. I’m not seeing a longstring input #1 type command? Am I just blind? Cheers Carl Retirement is tough on Hobbies without a day job |
||||
carlschneider Senior Member Joined: 04/08/2023 Location: South AfricaPosts: 158 |
Hi Peter Sub SettingsFile Open "settings.dat" for output As #1 Print #1, "{"; For ii=97 To 121 Print Chr$(ii) Print Eval(Chr$(ii)) <---------------------------------------------337 Print #1, "\q";Chr$(ii);"\q:";"\q";Str$(Eval(Chr$(ii)));",\q" Next ii 'code for z and final closing } Close #1 End Sub Should produce a JSON formatted file from a to y that looks like {"a":"10", .... But the interpreter is complaining with a [337] Print Eval(Chr$(ii)) Error : Array dimensions > Any ideas on what I missing? Cheers Carl Retirement is tough on Hobbies without a day job |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9110 |
Works for me. Have you somehow got eval set as a variable? |
||||
carlschneider Senior Member Joined: 04/08/2023 Location: South AfricaPosts: 158 |
Hi Peter I have checked for an Eval declaration but not seeing any. I will check if a has a value and what it is? I can’t thinking anything else that will cause the error. If all else fails I’ll try on a clean Pico. Have you had a chance to look at the SSD1306I2C issue causing the additional error messages and display not configured console output that Harm mentioned? The only way I can run edited code is to reset the Pico. It’s almost as if the program pointer or token table is corrupt, even if the code is correct? Cheers Carl Retirement is tough on Hobbies without a day job |
||||
carlschneider Senior Member Joined: 04/08/2023 Location: South AfricaPosts: 158 |
Thanks for the assistance Peter My problem is variable declaration clashes. I’ll have to rethink my approach to assembling the settings.dat JSON string. Cheers Carl Retirement is tough on Hobbies without a day job |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
another way to indirectly reference variable content is to use the DATA statement BASICs seem to be fairly evenly split on those that have DATA as absolute data, and those that have an element of EVAL. Speccy basic was like the latter, Locomotive Basic (CPC) was like the former. MMBasic also follows the evaluation method so you can read a word that shares it's name with a variable and you get the value in the variable. This is very useful in circumstances where the DATA needs to change with application. consider the following: Dim integer a,b,c,z,q a=10 b=15 c=170 For q=1 To 3 Read z Print z Next Data a,b,c Although it seems to have fallen out of fashion, I still put a lot of faith in small amounts of data saved in a program with DATA, READ and RESTORE, especially so in systems that have no native filesystem and so lack the ability to read a config file. EDIT: I tend to concentrate on the MicroMite - PicoMite mileage might vary... perhaps someone could test and clarify in the interests of shared knowledge? I suspect that, given this is an ancient bit of code, it will have been carried forward on the later platforms, but happy to be proved wrong. Edited 2024-07-29 05:26 by CaptainBoing |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2135 |
PicoMite MMBasic Version 5.09.00RC7 > AUTOSAVE Dim integer a,b,c,z,q a=10 b=15 c=170 For q=1 To 3 Read z Print z Next Data a,b,c Saved 117 bytes > RUN 10 15 170 |
||||
carlschneider Senior Member Joined: 04/08/2023 Location: South AfricaPosts: 158 |
Thank you Peter, CaptainBoing and Phil99 I also considered the VAR option but these are all susceptible to being potentially erased with a firmware update. While elegant and compact to index the loop using a to z the practical implementation proved challenging because I was trying to avoid an array and the variable names are meaningful. I resorted to using a to z as the JSON string field identifiers to simplify and compact the JSON string. (It looks like Line Input #1, buffer$ doesn’t support a longstring target). The JSON string assembly is then hard coded and written variable by variable to the file stored on A: When required it is read from A: with the same a to z index but again hardcoded using JSON$ extraction into the meaningfully named variables. The project runs both (ultimately) hardware and external watchdogs so the code needs to be self sufficient across resets. I’m thinking I might need to do some sort of CRC/checksum on the file or am I being paranoid … Cheers Carl Retirement is tough on Hobbies without a day job |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
completely irrelevant, leftfield thinking about my comment here: My wording was really clumsy... in my defence, I am in good company as the MMBasic manual says "[DATA] Stores numerical and string constants ... [and can be] ... expressions such as 5 * 60." this is also a bit iffy as the DATA is far from constant (it doesn't mention that variable names also work). Beams of enlightenment often come from unexpected angles... I was flitting through some old ZX81 docs (don't judge, I have my reasons ) and I came across this: "... a DATA statement is a list of expressions ..." A much more accurate description (for relevant Basics). However, this is is taken from an ill-advised treatise on not needing READ, DATA & RESTORE (absent from ZX81 Basic and someone is trying to justify it) as one can simply remember to type "GOTO 10" instead of RUN (ZX Basic saved the variables with the program). The outrageous expectation being that you set up the variables, save the prog and then use GOTO when you load the program. Somehow this is a demonstration of how modern ZXBASIC is in dispensing with R,D&R... I wonder how many times such progs had to be reloaded because the perfectly acceptable RUN had destroyed the variable space of the newly loaded code. just some ramblings... h |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6781 |
I still like Sinclair BASIC. It was unusual, but that's because there were few preconceptions in its design. It had to support the hardware and fit into a pretty small ROM. Incidentally, Tandy Level II BASIC also worked the same way, using GOTO to preserve variables and RUN to clear them. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
really, there wasn't much wrong with it. It was primitive but compared to TinyBASIC it was a dream. Missing bits can't really be held against it - either it has them or it doesn't HEX$,BIN$ - really useful but missing... just different. I really liked the string slicing even though there was confusing syntax e.g. A$(3) - do I mean the third element in an array or the third character in a string? but A$(1 TO 5) just has a nice feel to it and requires only a single mechanism rather than the three of more modern BASICs (given; MID$ can replace LEFT$ & RIGHT$ with a little thought) h |
||||
Print this page |