[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: passing structure elements... by value?



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...
>