Re: dynamically naming variables [message #34357 is a reply to message #34356] |
Mon, 03 March 2003 15:22   |
Pavel Romashkin
Messages: 166 Registered: April 1999
|
Senior Member |
|
|
Don't use runtime name definition. I was told by a developer at RSI
that, if you find the need for this in your program, this means you need
a better design. I am ever so thankful for this advice.
If you use runtime naming, you will have to use Execute again to refer
to the variable. And you will get into a lot of trouble the next time
you have to reuse the program, or pass that variable out of the program.
Try making a structure instead:
varnames=['temp1','temp2','temp3']
struct = {name : '', value : ptr_new(/allocate)}
data = replicate(struct, n_elements(varnames))
for i=0, n_elements(varnames)-1 do begin
data.name = varnames[i]
*data.value = bytarr(25) ; Or anything at all.
endfor
Now, you can refer to your data using the name tag of the structure.
Locate the right structure in the array using WHERE and use the result
to get to VALUE.
This is not the only way. You can use the linked list from Dave
Fanning's web site.
Another simple way I can see that will avoid searching with WHERE would
use a... widget tree. Create one and populate children's UVALUE with the
data. Then, you can search using /FIND_BY_UNAME keyword to Widget_Info.
I am not sure it is more efficient than WHERE, but takes about 5 lines
of code less :-)
Cheers,
Pavel
Steve Nesbitt wrote:
>
> Howdy folks,
>
> I was wondering if there was a way to dynamically name a variable in
> IDL, something like:
>
> varnames=['temp1','temp2','temp3']
>
> for i=0, n_elements(varnames)-1 do begin
>
> `varnames[i]`=bytarr(25)
>
> endfor
>
> I would appreciate any help on this, or a way to get around this issue.
>
> Cheers,
> -Steve
>
> --
> ____________________________________________________________ _________
> Steve Nesbitt, Ph.D. Radar Meteorology Group Rm. 203
> Department of Atmospheric Science
> Colorado State University
> Fort Collins, CO 80523-1371
> Phone: (970) 491-6944
> Fax: (970) 491-8449
> E-mail: snesbitt@radarmet.atmos.colostate.edu
> Web: http://radarmet.atmos.colostate.edu/~snesbitt
|
|
|