RE: Converting a string to a variable name

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here's my two [late] cents, since I didn't check my mail this weekend.

> -----Original Message-----
> From: rubis at fem.unicamp.br [mailto:rubis at fem.unicamp.br]

> Hi all;
> 
> Direct to the point...
> 
> 	I'm writing a program that uses a lot of sequencial variables:
> 
> a1, a2, a3...
> 
> and give values for then:
> 
> a1=1, a2=2, a3=3 ...(for example)
> 
> 
> I would like to do this:
> 
> atom a1,a2,a3
> sequence s
> 	for x=1 to 3 do
> 
> 		s=sprintf("%d",x)
> 		"a"&s=x                           -- atribute a 
> "variable variable" name 
> a variable value  :>))
> 
> 	end for
> 
> but this does not work because "a"&s is a string, not a variable.
> 
> How I can convert a string to a variable name ???

You could do exactly this with my modified interpreter (check the recent
contributions).  Although this particular interest would be better served
simply using arrays, as many have pointed out, you could also do this:

-- example
include variable.em
atom a1, a2, a3
sequence s
integer id

for x = 1 to 3 do
  s = sprintf("%d",x)
  id = variable_id( "a" & s )

  if id >= 0 then
    write_variable( id, x )
  end if

end for
-- end example

Matt Lewis

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu