Variable value by its name representation as string

By Eugeny_Brychkov

Paragon (1150)

Eugeny_Brychkov's picture

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 or register to post comments

By Meits

Scribe (5892)

Meits's picture

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.

By Gabbiani

Supporter (13)

Gabbiani's picture

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's picture

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 (16689)

Manuel's picture

07-06-2018, 00:07

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

By Arjan

Paladin (717)

Arjan's picture

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.

By NYYRIKKI

Enlighted (5556)

NYYRIKKI's picture

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

By brawaga

Supporter (11)

brawaga's picture

08-07-2020, 15:10

«VAL» could be your friend, Eugeny, but on ZX Spectrum. Usage like «x=val(a$+"(1)")» would evaluate an expression. Unfortunately, MSX BASIC parses numbers only. I am in search for solution for same problem, but also I want to assign variables given by a name in string variable. I have some kind of tricky solution as self-modifying BASIC program, but I hope there is more natural ways.

UPD: https://www.msx.org/wiki/VARPTR() should work both directions, if only you can interpret bytes of how variable stored. I think it is enough for my task, hence I needed this for integers only, but maybe will be useful for you too.

By Grauw

Ascended (9156)

Grauw's picture

08-07-2020, 14:49

Or if a$="a" then x=a(1) else if a$="b" then x=b(1) else x=c(1).

By brawaga

Supporter (11)

brawaga's picture

08-07-2020, 15:56

This might become too long for all possible 1-letter variables. But MSX-BASIC supports two-letter variables also. It multiplicates with different suffixes for different variable types, but even without we can probably go out of available program RAM, not talking about just fingers wear off typing it.
By the way, I just tried my variant and it definitely won't work: VARPTR accepts var itself, not a quoted name. So I will examine how variables work on low level.

By pgimeno

Master (188)

pgimeno's picture

10-07-2020, 04:09

Well, I don't recommend it, but if you absolutely must, here's my solution:

40 defusr5=&hd000:x=usr5(a$+"(1)")

With this relocatable routine loaded at 0D000h:

ERRBAS		equ	406Fh
;PTRGET		equ	4E9Bh
PTRGET		equ	5EA4h
VMOVFM		equ	2F08h
VALTYP		equ	0F663h
DAC		equ	0F7F6h

		ld	a,(VALTYP)
		cp	3
		jr	nz,ErrD
		ld	de,(DAC+2)
		ld	hl,0E9E1h
		ld	(DAC+2),hl
		call	DAC+2
		ld	bc,buf-$
		add	hl,bc
		ex	de,hl
		ld	(DAC+2),hl
		ld	b,(hl)
		xor	a
		cp	b
		jr	z,Err18
		inc	hl
		ld	a,(hl)
		inc	hl
		ld	h,(hl)
		ld	l,a
		push	de
UpperLoop:	ld	a,(hl)
		inc	hl
		cp	'a'
		jr	c,NotLower
		cp	'z'+1
		jr	nc,NotLower
		add	a,'A'-'a'
NotLower:	ld	(de),a
		inc	de
		djnz	UpperLoop
		xor	a
		ld	(de),a
		pop	hl
		call	PTRGET
		ex	de,hl
		ld	(DAC+2),hl
		rst	28h
		call	nz,VMOVFM
		ret
ErrD:		ld	e,0Dh
		jp	ERRBAS
Err18:		ld	e,18h
		jp	ERRBAS

buf:		ds	256

It takes 336 bytes and lets you examine any variable.