Variable value by its name representation as string

Door Eugeny_Brychkov

Paragon (1232)

afbeelding van 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.

Aangemeld of registreer om reacties te plaatsen

Van meits

Scribe (6573)

afbeelding van 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.

Van Gabbiani

Supporter (13)

afbeelding van 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.

Van robylu

Supporter (6)

afbeelding van 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?

Van Manuel

Ascended (19683)

afbeelding van Manuel

07-06-2018, 00:07

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

Van Arjan

Paladin (787)

afbeelding van 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 (6093)

afbeelding van 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

Van brawaga

Resident (58)

afbeelding van brawaga

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: 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.

Van Grauw

Ascended (10821)

afbeelding van Grauw

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).

Van brawaga

Resident (58)

afbeelding van brawaga

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.

Van pgimeno

Champion (328)

afbeelding van pgimeno

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
ErrD:		ld	e,0Dh
Err18:		ld	e,18h

buf:		ds	256

It takes 336 bytes and lets you examine any variable.