temporary() problem [message #86024] |
Tue, 24 September 2013 13:25  |
spluque
Messages: 33 Registered: September 2013
|
Member |
|
|
Hi,
I'm not sure I understand why the variable 'a' below gets undefined after calling a procedure using temporary():
IDL> a='a, b, c'
IDL> help, a
A STRING = 'a, b, c'
IDL> .run "test.pro"
% Compiled module: TEST.
IDL> test, a
IDL> help, a
A UNDEFINED = <Undefined>
The contents of test.pro:
PRO TEST, str
ostr=strsplit(temporary(str), ', ', /extract)
END
Why is temporary() messing with the top level environment?
Seb
|
|
|
Re: temporary() problem [message #86025 is a reply to message #86024] |
Tue, 24 September 2013 13:32   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
spluque@gmail.com writes:
> I'm not sure I understand why the variable 'a' below gets undefined after calling a procedure using temporary():
>
> IDL> a='a, b, c'
> IDL> help, a
> A STRING = 'a, b, c'
> IDL> .run "test.pro"
> % Compiled module: TEST.
> IDL> test, a
> IDL> help, a
> A UNDEFINED = <Undefined>
>
>
> The contents of test.pro:
>
> PRO TEST, str
> ostr=strsplit(temporary(str), ', ', /extract)
> END
>
>
> Why is temporary() messing with the top level environment?
Because variables are passed by reference in IDL, not by value (copied
into the procedure). In this case, you are working with the variable str
itself, not a copy of the variable.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: temporary() problem [message #86026 is a reply to message #86025] |
Tue, 24 September 2013 14:29   |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
On 09/24/13 16:32, David Fanning wrote:
> spluque@gmail.com writes:
>
>> I'm not sure I understand why the variable 'a' below gets undefined after calling a procedure using temporary():
>>
>> IDL> a='a, b, c'
>> IDL> help, a
>> A STRING = 'a, b, c'
>> IDL> .run "test.pro"
>> % Compiled module: TEST.
>> IDL> test, a
>> IDL> help, a
>> A UNDEFINED = <Undefined>
>>
>>
>> The contents of test.pro:
>>
>> PRO TEST, str
>> ostr=strsplit(temporary(str), ', ', /extract)
>> END
>>
>>
>> Why is temporary() messing with the top level environment?
>
> Because variables are passed by reference in IDL, not by value (copied
> into the procedure). In this case, you are working with the variable str
> itself, not a copy of the variable.
I would go with the explanation that temporary() is "messing" with the
top level environment because that's what the programmer wrote it to do.
"Why is the computer doing what I told it to do, not what I want it to do?"
:o)
For the OP, try this for some obfuscatory fun:
IDL> a=['qwerty','a,b,c']
IDL> help, a
A STRING = Array[2]
IDL> print, a
qwerty a,b,c
IDL> .run "test.pro"
% Compiled module: TEST.
IDL> test, a[1]
IDL> print, a
|
|
|
|
Re: temporary() problem [message #86028 is a reply to message #86027] |
Tue, 24 September 2013 15:43  |
spluque
Messages: 33 Registered: September 2013
|
Member |
|
|
Thanks to both of you! Clearly, I wasn't paying attention to the book section on argument passing mechanism. Now I've learnt not to use temporary() directly on arguments!
Cheers,
Seb
On Tuesday, September 24, 2013 4:39:39 PM UTC-5, David Fanning wrote:
> Paul van Delst writes:
>
>
>
>> I would go with the explanation that temporary() is "messing" with the
>
>> top level environment because that's what the programmer wrote it to do.
>
>>
>
>> "Why is the computer doing what I told it to do, not what I want it to do?"
>
>
>
> This comes later in the syllabus, when we talk about "programming
>
> philosophy, prayer, and metaphysics." ;-)
>
>
>
> Cheers,
>
>
>
> David
>
>
>
> --
>
> David Fanning, Ph.D.
>
> Fanning Software Consulting, Inc.
>
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
>
> Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|