RE: Converting a string to a variable name

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

irv at take.maxleft.com wrote:
> 
> rubis at fem.unicamp.br wrote:
> > 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 can't, but is there any reason you cannot use an array of 
> atoms?
> 
> object a
>   a[1] = 1
>   a[2] = 2
>    ...etc
> 
> for x = 1 to 3 do
>   a[x] = ??
> 

I suppose I should mention that you'll need to either dimension the 
array before using it: 
 a = repeat(0,4)
or 
 a = {0,0,0,0}
or
 a = {1,2,3,4} -- create and initialize in one step.

If you don't know how many numbers you'll be storing, you 
can create an 'empty' array:
 a = {}
and then append each new number to it.

a = {}
for x = 1 to 4 do
  a = append(a,x)
end for

a will now look like this: {1,2,3,4}

Irv

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

Search



Quick Links

User menu

Not signed in.

Misc Menu