1. Converting a string to a variable name
- Posted by rubis at fem.unicamp.br
Jul 28, 2002
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 ???
Thanks
Rubens M. Luciano, Eng.
State Universisty of Campinas - UNICAMP
Sao Paulo
Brazil
2. Re: Converting a string to a variable name
Hello Rubens,
----------
> Îò: rubis at fem.unicamp.br
> Êîìó: EUforum <EUforum at topica.com>
> Òåìà: Converting a string to a variable name
> Äàòà: 28 èþëÿ 2002 ã. 20:49
>
> 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 ???
>
> Thanks
>
> Rubens M. Luciano, Eng.
> State Universisty of Campinas - UNICAMP
> Sao Paulo
> Brazil
Try please:
----name.ex
sequence a
a={}
for x=1 to 3 do
a &=x
end for
? a
? a[1]
a[1] += 5
a[2] -= 10
a[3] *= 15
? a[3]
? a
----end of name.ex
Or give us please more detailed description
of your *real* task.
Regards,
Igor Kachan
kinz at peterlink.ru
3. Re: Converting a string to a variable name
On 28 Jul 2002, at 13:49, 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 ???
After you got 3 examples of indexing a sequence, you know it cannot be
done. I do this in mirc a *lot*, since i cannot know beforehand what the
assignments will be:
if ( +p+ isin %nickstat. [ $+ [ $remove($mask($address(%clones.nick ,5),1),
* ) ] $+ . $+ [ # ] ] ) { goto skipclonekick1 }
However, you can do this in Eu:
sequence names
sequence data
for x=1 to 3 do
s=sprintf("%d",x)
if length(names) <= x then
names &= {}
data &= {}
end if
names[x] = sprintf("%d",x) -- "kat" works too
data[x] = x -- give it any object
end for
Then to locate the data by name in euphoria, you use:
function getdata(sequence var)
atom isit
isit = find(var,names)
if (isit != 0)
then return data[isit]
else return ""
end if
end function
value = getdata("kat")
Kat
4. Re: Converting a string to a variable name
Reubens wrote:
> How I can convert a string to a variable name ???
You can't. This is one of Kat's pet peeves.
-- David Cuny
5. Re: Converting a string to a variable name
On 24 Jul 2002, at 19:56, David Cuny wrote:
>
> Reubens wrote:
>
> > How I can convert a string to a variable name ???
>
> You can't. This is one of Kat's pet peeves.
I *hope* this is not the reason we can't do it. If it is, can someone else
please think up my pet peeves again, and resubmit them as good ideas to
Rob? Thanks.
Kat
6. Re: Converting a string to a variable name
I haven't tried it but Matthew Lewis has posted a modified interpreter
which lets you use a function similar to routine_id() for variables.
Pete
7. Re: Converting a string to a variable name
Kat wrote:
> If it is, can someone else please think up my pet peeves again,
> and resubmit them as good ideas to Rob? Thanks.
Hey, I didn't say they were bad ideas.
Well, perhaps the GOTO.
-- David Cuny