Re: Creating Variables in Programs [message #12188] |
Fri, 10 July 1998 00:00 |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
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...
Martin.
--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/
------------------------------------------------------------ -------
|
|
|
Re: Creating Variables in Programs [message #12193 is a reply to message #12188] |
Fri, 10 July 1998 00:00  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Martin Schultz <mgs@io.harvard.edu> writes:
>
> Perry Phillips wrote:
>>
>> Does anyone know of a way to create a new array under program control, ie
>> create a string and use that string to make an array. As far as I can see
>> this is impossible in IDL?
>>
>> --
>> Perry Phillips p.phillips@mail.utexas.edu
>
>
> here's a quick example
>
>
> 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:
A = 0
...
R = EXECUTE('A=FLTARR(200)')
The IDL internal compiler needs to know that "A" exists before it can
be assigned to in an EXECUTE statement. The same applies for
restoring variables: all the variables in the SAVE file must be
predefined in the procedure. Assigning zero to them is fine.
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@astrog.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: Creating Variables in Programs [message #12194 is a reply to message #12188] |
Fri, 10 July 1998 00:00  |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Perry Phillips wrote:
>
> Does anyone know of a way to create a new array under program control, ie
> create a string and use that string to make an array. As far as I can see
> this is impossible in IDL?
>
> --
> Perry Phillips p.phillips@mail.utexas.edu
here's a quick example
IDL> r=execute('a=fltarr(200)')
IDL> help,a
A FLOAT = Array[200]
Martin.
--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
109 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
Internet-homepage: http://www-as.harvard.edu/people/staff/mgs/
------------------------------------------------------------ -------
|
|
|