Variable value by its name representation as string

By Eugeny_Brychkov

Paragon (1107)

Аватар пользователя 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.

Для того, чтобы оставить комментарий, необходимо регистрация или !login

By Meits

Scribe (5665)

Аватар пользователя 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:

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

By Gabbiani

Supporter (13)

Аватар пользователя 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.

By robylu

Supporter (6)

Аватар пользователя 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?

By Manuel

Ascended (15841)

Аватар пользователя Manuel

07-06-2018, 00:07

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

By Arjan

Paladin (714)

Аватар пользователя Arjan

07-06-2018, 09:58

I guess it should possible by calling some basic interpreter functions, like in the thread below:

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


Enlighted (5402)

Аватар пользователя 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