Barry Brevik wrote:
UV 9.6.1.3 on Windows.
For the longest time this has been bugging me, but right now I could really use this capability.
Does anyone know if it is possible to reference a variable indirectly? IOW, I want to be able to store variable names in a file (for example), and then assign values to those variables as the program encounters them.
For example, something like this:
PARTNO = ''; LOTNO = ''; PARTNAME = '' VAR.NAMES = 'PARTNO':@FM:'LOTNO':@FM:'PARTNAME' * * Next, a magic command that makes the variable * refered to by VAR.NAMES<2> equal to 'it works'. (VAR.NAMES<2>) = 'it works'; * I know this does not work, just example. PRINT LOTNO * * Variable LOTNO is now eq 'it works'.
Possible??
Barry
No there is no way to do that. You'd need an 'eval' function/statement.
But You can do something similar with a subroutine and a function.
Subroutine SET( variable name, value )
Function VAL( variable name )
They need to share a common ( or a file ) where variable-value pairs are stored.
A straight forward implementation would be to search for the variable name
in an mv-string and use the index to look up the value from another mv
string. If you know the number of entries you can use arrays.
Even if you don't you can still use arrays by running imformation style allowing
them to be redimensioned.
Then Your program would look like:
DEFFUN VAL( var ) CALLING "VAL"
CALL SET( 'LOTNO', 'it works' )
PRINT VAL( 'LOTNO' )
----------------------------
Naive untested implementation. If heavily used you'd need to hash e.g. as MOD( SEQ(VAR[1,1])*LEN(VAR), N )+1 where N is the number of hash points and the dimension of VARS and VALS.
SUBROUTINE SET( VAR, VAL)
COMMON /..SET/ VARS, VALS
LOCATE VAR IN VARS<1> SETTING II THEN VALS<II> = VAL END ELSE VARS<-1> = VAR VALS<-1> = VAL END RETURN END
FUNCTION VAL( VAR ) !! Hashed version COMMON /..SET/ VARS(N), VALS(N)
HASH = MOD( SEQ(VAR[1,1])*LEN(VAR), N )+1
RES = "" LOCATE VAR IN VARS(HASH)<1> SETTING II THEN RES = VALS(HASH)<II> END RETURN (RES) END
-- mats ------- u2-users mailing list u2-users@listserver.u2ug.org To unsubscribe please visit http://listserver.u2ug.org/