Re: dynamically naming variables [message #34356] |
Mon, 03 March 2003 16:10 |
Steve Nesbitt
Messages: 4 Registered: March 2003
|
Junior Member |
|
|
Pavel Romashkin wrote:
> 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.
Thanks for the much more elegant alternative. I use structures
regularly, but was just looking for a quick fix for a problem that
required a read/write of several gigabytes of data. I won't do it
again, I promise! I won't! :)
Cheers,
-Steve
|
|
|
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
|
|
|
Re: dynamically naming variables [message #34361 is a reply to message #34357] |
Mon, 03 March 2003 13:56  |
Steve Nesbitt
Messages: 4 Registered: March 2003
|
Junior Member |
|
|
ahhh...thanks! Saves me from dynamically generating the code each time
using a shell script (a major pain).
Paul van Delst wrote:
> 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.
>
>
> Use the EXECUTE command to execute a command you create in a string variable.
>
> paulv
>
|
|
|
Re: dynamically naming variables [message #34362 is a reply to message #34361] |
Mon, 03 March 2003 13:48  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
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.
Use the EXECUTE command to execute a command you create in a string variable.
paulv
--
Paul van Delst
CIMSS @ NOAA/NCEP/EMC
Ph: (301)763-8000 x7748
Fax:(301)763-8545
|
|
|