Variable value by its name representation as string

Por Eugeny_Brychkov

Paragon (1106)

imagem de Eugeny_Brychkov

06-06-2018, 16:24

I am stuck at the stupidest problem - can not figure out how to get variable value. See the code below

5 a(1)=2:b(1)=10:c(1)=100
10 data "a","b","c"
15 restore 10
20 for i=0 to 2
30 read a$
40 x=... ???
50 print x
60 next i

I define list of variable names using DATA, each of these variables are arrays. I need to take value of a(1) according to read of first string from data, b(1) according to reading second, etc. What ...??? must be to convert a$ being "a" to value of a(1)?

I have a suspicion that it may simply be impossible at BASIC level. Please advise.

Entrar ou registrar-se para comentar

Por Meits

Scribe (5651)

imagem de Meits

06-06-2018, 17:42

i deleted line 5 and put its value in line 10 as string, so between quotes. then line 40 became x=val(a$). That gave the following output:
2
10
100

i don't expect the variables in line 5 will go into the strings in line 10.

Por Gabbiani

Supporter (13)

imagem de Gabbiani

06-06-2018, 18:31

I am not an expert with BASIC, but I'd say it is not possible for a BASIC program to evaluate a string as an expression.

Por robylu

Supporter (6)

imagem de robylu

06-06-2018, 19:31

I don't know if i am saying something wrong, but i would convert the "a" in the ascii code with the ASC command and then evaluate the result with an if. Too long way?

Por Manuel

Ascended (15821)

imagem de Manuel

07-06-2018, 00:07

Indeed, strings can't be evaluated as variable names, as far as I know Smile

Por Arjan

Paladin (714)

imagem de Arjan

07-06-2018, 09:58

I guess it should possible by calling some basic interpreter functions, like in the thread below:
https://www.msx.org/forum/msx-talk/development/invoking-the-...

It's easier to just use one multidimensional array and change the data to indexes.

Por NYYRIKKI

Enlighted (5398)

imagem de NYYRIKKI

07-06-2018, 10:31

Nothing really to add here, I think Arjan covered your options... MSX-BASIC does not natively have anything like EXECUTE-command.

Working version of your listing would be:

1 dim d(2,10)
5 d(0,1)=2:d(1,1)=10:d(2,1)=100
10 data 0,1,2
15 restore 10
20 for i=0 to 2
30 read p
40 x=d(p,1)
50 print x
60 next i