| Re: Is it possible to set a string to a veriable name? [message #67939] |
Fri, 04 September 2009 07:15 |
rtk
Messages: 22 Registered: September 2008
|
Junior Member |
|
|
On Sep 4, 5:36 am, "dux...@gmail.com" <dux...@gmail.com> wrote:
> I have an array of string and want to use its elements as veriable
> names in a function.
> How can I do?
>
> Is there any simple code to replace the ones below?
>
> For example:
> STR = ['a', 'b', 'c', 'd', 'e']
> for i=0, 4 do begin
> case i of
> 0: data=a
> 1: data=b
> 2: data=c
> 3: data=d
> 4: data=e
> endcase
> ..............
> ..............
> ..............
> endfor
Use scope_varfetch:
IDL> str = ['a','b','c','d']
IDL> for i=0,3 do (scope_varfetch(str[i],/ENTER,LEVEL=1)) = i
IDL> print, b
1
IDL> print, a
0
IDL> print, d
3
Setting LEVEL to 1 creates the new variables in the main level, not
specifying LEVEL or setting it to 0 will create them in the current
routine.
Ron
|
|
|
|
| Re: Is it possible to set a string to a veriable name? [message #67941 is a reply to message #67939] |
Fri, 04 September 2009 06:26  |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
On Sep 4, 6:36 am, "dux...@gmail.com" <dux...@gmail.com> wrote:
> I have an array of string and want to use its elements as veriable
> names in a function.
> How can I do?
>
> Is there any simple code to replace the ones below?
>
> For example:
> STR = ['a', 'b', 'c', 'd', 'e']
> for i=0, 4 do begin
> case i of
> 0: data=a
> 1: data=b
> 2: data=c
> 3: data=d
> 4: data=e
> endcase
> ..............
> ..............
> ..............
> endfor
You could use EXECUTE(). Something like:
result = execute('data='+STR[i])
'course it won't work in the vm...
|
|
|
|