comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » passing structure elements... by value?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
passing structure elements... by value? [message #25725] Fri, 13 July 2001 09:03 Go to next message
Randall Skelton is currently offline  Randall Skelton
Messages: 169
Registered: October 2000
Senior Member
Hi all,

I am seem to be somewhat confused on passing structure... this *almost*
seems like one of those dreaded 'pass by reference/pass by value'
problems...

I have a simple routine that increments the value of a structure:

pro test, mod_struct
for i=0, n_tags(mod_struct)-1 do begin
mod_struct.(i) = mod_struct.(i) + 1
endfor
end

As expected, this will increment all of the values in a passed structure.

IDL> struct = {A:0, B:0} & big = replicate(struct,3)
IDL> print, big
{ 0 0}{ 0 0}{ 0 0}
IDL> test, big
IDL> print, big
{ 1 1}{ 1 1}{ 1 1}

But, when I try and increment a single element in the structure it fails?

IDL> struct = {A:0, B:0} & big = replicate(struct,3)
IDL> test, big[0]
IDL> print, big
{ 0 0}{ 0 0}{ 0 0}
^^^^^^^^^^^^^^^^^^

I expected the ^^ element above to be ones? Is there any way to force IDL
to pass this by reference instead of passing by value? It would be nice if
you could put brackets around the thing you want to pass by reference...
something like '(big[0])'

Have a good weekend all!
Randall

PS: I'm sure Liam's new book answers this...
Re: passing structure elements... by value? [message #25796 is a reply to message #25725] Tue, 17 July 2001 08:54 Go to previous message
Vince Hradil is currently offline  Vince Hradil
Messages: 574
Registered: December 1999
Senior Member
Not sure this is a structure issue, but instead an array issue!
Array elements are passed by value. Instead you have to do:
IDL> struct = {A:0, B:0} & big = replicate(struct,3)
IDL> tmpbig = big[0]
IDL> test, tmpbig
IDL> big[0] = tmpbig
IDL> print, big
{ 1 1}{ 0 0}{ 0 0}
Or how about changing your pro to a function?
function test, mod_struct
for i=0, n_tags(mod_struct)-1 do begin
mod_struct.(i) = mod_struct.(i) + 1
endfor
return, mod_struct
end
then do:
IDL> struct = {A:0, B:0} & big = replicate(struct,3)
IDL> big[0] = test(big[0])
IDL> print, big
{ 1 1}{ 0 0}{ 0 0}
NB I haven't tested any of the above code, since I am away from my "IDL computer" at the moment ;^)

On Fri, 13 Jul 2001 17:03:39 +0100, Randall Skelton <rhskelto@atm.ox.ac.uk> wrote:
> Hi all,
>
> I am seem to be somewhat confused on passing structure... this *almost*
> seems like one of those dreaded 'pass by reference/pass by value'
> problems...
>
> I have a simple routine that increments the value of a structure:
>
> pro test, mod_struct
> for i=0, n_tags(mod_struct)-1 do begin
> mod_struct.(i) = mod_struct.(i) + 1
> endfor
> end
>
> As expected, this will increment all of the values in a passed structure.
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> IDL> test, big
> IDL> print, big
> { 1 1}{ 1 1}{ 1 1}
>
> But, when I try and increment a single element in the structure it fails?
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> test, big[0]
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> ^^^^^^^^^^^^^^^^^^
>
> I expected the ^^ element above to be ones? Is there any way to force IDL
> to pass this by reference instead of passing by value? It would be nice if
> you could put brackets around the thing you want to pass by reference...
> something like '(big[0])'
>
> Have a good weekend all!
> Randall
>
> PS: I'm sure Liam's new book answers this...
>
Re: passing structure elements... by value? [message #25809 is a reply to message #25725] Mon, 16 July 2001 02:45 Go to previous message
Jaco van Gorkom is currently offline  Jaco van Gorkom
Messages: 97
Registered: November 2000
Member
Randall Skelton wrote:
> I have a simple routine that increments the value of a structure:
>
> pro test, mod_struct
> for i=0, n_tags(mod_struct)-1 do begin
> mod_struct.(i) = mod_struct.(i) + 1
> endfor
> end
>
> As expected, this will increment all of the values in a passed structure.
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> IDL> test, big
> IDL> print, big
> { 1 1}{ 1 1}{ 1 1}
>
> But, when I try and increment a single element in the structure it fails?
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> test, big[0]
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> ^^^^^^^^^^^^^^^^^^
>
> I expected the ^^ element above to be ones? Is there any way to force IDL
> to pass this by reference instead of passing by value? It would be nice if
> you could put brackets around the thing you want to pass by reference...
> something like '(big[0])'

Note that in your example you are passing one *array* element of an array of
structures. Indexed array elements are also passed by value - see Liam's list.
One workaround would be to copy the array element(s) into a temporary variable:
working_elements = big[range]
test, working_elements
big[range] = working_elements
, with range being a vector of indices, just [0] in the example.
Another workaround involves passing the indices-of-interest into the routine.

cheers,
Jaco

----------------
Jaco van Gorkom gorkom@rijnh.nl
FOM-Instituut voor Plasmafysica `Rijnhuizen', The Netherlands
Re: passing structure elements... by value? [message #25823 is a reply to message #25725] Fri, 13 July 2001 09:53 Go to previous message
david[2] is currently offline  david[2]
Messages: 100
Registered: June 2001
Senior Member
Randall Skelton writes:

> I am seem to be somewhat confused on passing structure... this *almost*
> seems like one of those dreaded 'pass by reference/pass by value'
> problems...
>
> I have a simple routine that increments the value of a structure:
>
> pro test, mod_struct
> for i=0, n_tags(mod_struct)-1 do begin
> mod_struct.(i) = mod_struct.(i) + 1
> endfor
> end
>
> As expected, this will increment all of the values in a passed structure.
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> IDL> test, big
> IDL> print, big
> { 1 1}{ 1 1}{ 1 1}
>
> But, when I try and increment a single element in the structure it fails?
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> test, big[0]
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> ^^^^^^^^^^^^^^^^^^
>
> I expected the ^^ element above to be ones? Is there any way to force IDL
> to pass this by reference instead of passing by value? It would be nice if
> you could put brackets around the thing you want to pass by reference...
> something like '(big[0])'

You could easily add the field you want to change as
a second positional parameter:

pro test, mod_struct, fieldno
IF N_Elements(filedno) EQ 0 THEN BEGIN
for I=0, n_tags(mod_struct)-1 do begin
mod_struct.(I) = mod_struct.(I) + 1
endfor
ENDIF ELSE mod_struct.(fieldno) = mod_struct.(fieldno) + 1
end

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
Re: passing structure elements... by value? [message #25824 is a reply to message #25725] Fri, 13 July 2001 09:39 Go to previous message
Liam E. Gumley is currently offline  Liam E. Gumley
Messages: 378
Registered: January 2000
Senior Member
Randall Skelton wrote:
> I am seem to be somewhat confused on passing structure... this *almost*
> seems like one of those dreaded 'pass by reference/pass by value'
> problems...
>
> I have a simple routine that increments the value of a structure:
>
> pro test, mod_struct
> for i=0, n_tags(mod_struct)-1 do begin
> mod_struct.(i) = mod_struct.(i) + 1
> endfor
> end
>
> As expected, this will increment all of the values in a passed structure.
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> IDL> test, big
> IDL> print, big
> { 1 1}{ 1 1}{ 1 1}
>
> But, when I try and increment a single element in the structure it fails?
>
> IDL> struct = {A:0, B:0} & big = replicate(struct,3)
> IDL> test, big[0]
> IDL> print, big
> { 0 0}{ 0 0}{ 0 0}
> ^^^^^^^^^^^^^^^^^^
>
> I expected the ^^ element above to be ones? Is there any way to force IDL
> to pass this by reference instead of passing by value? It would be nice if
> you could put brackets around the thing you want to pass by reference...
> something like '(big[0])'
>
> Have a good weekend all!
> Randall
>
> PS: I'm sure Liam's new book answers this...

It sure does: see pages 114-115.

Structure elements are passed by value. Therefore they cannot be
modified by a called procedure or function. To be more precise, the
called routine makes a copy of all input arguments, and the copies may
be modified during the lifetime of the called routine. When control is
returned to the caller, the copies of arguments that were passed by
value are copied back to the corresponding calling arguments. The copies
of arguments that were passed by value are destroyed.

The following entities are passed by reference:
Scalars
Arrays
Structures
Undefined variables

The following entities are passed by value:
Constants
Indexed subarrays
Structure elements
System variables
Expressions

Cheers,
Liam.
Practical IDL Programming
http://www.gumley.com/
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Events in IDLDrawWidget
Next Topic: widget-programming

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 14:53:10 PDT 2025

Total time taken to generate the page: 0.11200 seconds