Re: Creating Variables in Programs [message #12303 is a reply to message #12195] |
Tue, 14 July 1998 00:00   |
R. Bauer
Messages: 137 Registered: November 1996
|
Senior Member |
|
|
Martin Schultz wrote:
> Craig Markwardt wrote:
>>
>>> IDL> r=execute('a=fltarr(200)')
>>> IDL> help,a
>>> A FLOAT = Array[200]
>>>
>>
>> There is a "gotcha." in the case of a compiled procedure, the
>> variable "a" must have already been defined. The following is usually
>> sufficient:
>> [...]
>
> Huh? Here is a little program:
>
> --------------------------------------
> pro testexec,name
>
> r=execute(name+'=findgen(10)')
>
> print,r
> print,b
> return
> end
> --------------------------------------
>
> Of course, you have to call it as testexec,'b' in order to have it work
> properly ;-), but it demonstrates that you don't have to have your
> variable initialized!!
>
> But I don't really see the point of the original question: why the h...
> do you want to do this? To my knowledge, creating variables only makes
> sense if you know what to do with them afterwards - and in order to do
> something with them, you must know their name beforehand. If you want to
> export your newly created variables to the main program or some other
> procedure, you would have to proceed completely different. I would
> create a structure with
> template = { name:'', pvalue:ptr_new() }
> (or an array of these structures with replicate(...) )
>
> then manipulatge the string 'name=expression' to 'tmp=expression', store
> the 'name' field in the name tag of the structure and
> pvalue=ptr_new(tmp) will save the value.
>
> This would act as a container (sounds awfully like OOP doesn't it ?),
> and you would have to do a lot of type and error checking in any routine
> that uses the information in this structure (array). Note, that IDL
> itself would not "know" anything about your variables - but, as I said,
> it doesn't make sense if it had to.
>
> ... and don't forget to clean up your heap once a while...
>
Hi Martin,
that's not totally correct.
idl knows a lot of your variables which are defined or defined as undefined
(a=n_elements(b))
print,routine_names(/variables)
for more look in the by now obsolete routine gethelp
I am using this mechanism to create a dynamical structure where are nearby
100 names with definitions (mostly descriptions for datasets like:
experiment,PI_name, param_long_name, param_units ...) are defined. And all
of them which are defined in a program will go into a structure.
In the program I have only to define param_units='K' and later on it will
be a tag name in a structure. All whats in the structure is could be
written to somewhere e.g. netCDF.
Reimar
--
R.Bauer
Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
|
|
|