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

Re: indexing over structure tags



Randall Skelton <rhskelto@atm.ox.ac.uk> writes:

> Hello,
> 
> Imagine someone has a structure of structures...
> 
>   ; define the basic structure for each 
>   sm_struc = {basic_struct, comment: ' ', values: fltarr(nlev)}
>   
>   ; define the large structure
>   data = {big_struct, so4: sm_struc, co2: sm_struc, hcl: sm_struc}
> 
> The IDL manual describes how to make an array of the tags in a structure
> using:
> 
>   ; get the names of the tags
>   names = tag_names(data)
> 
> so that names = [so4, co2, hcl].
> 
> That is all fine.  But is it possible to index over the tag names with a
> for-loop?
> 
> i.e.   for i=0, n_elements(names)-1 do data.names[i].values = i
>                                        ^^^^^^^^^^^^^^^^^^^^

> 
> where IDL determines what the appropriate label 'data.name[i].values' is.  
> 
> Thanks,
> Randall
> 

  data.(i) to iterate over that tags of data, and 
  data.(i).(j) to iterate over the tags of data.(i)

If each tag is an array, rather than a scalar structure instance, you
might (probably?) have to pull it out, like this

IDL> junk={foo, a:0, b:0l, c:0.0}
IDL> bar={bar, a:replicate({foo},3),b:replicate({foo},4)}
IDL> help,bar,/struct
** Structure BAR, 2 tags, length=84:
   A               STRUCT    -> FOO Array[3]
   B               STRUCT    -> FOO Array[4]


IDL> .run
- FOR i=0,1 DO BEGIN 
-   FOR j=0,2 DO BEGIN 
-    tmp=bar.(i).(j) ;<--- I couldn't find a way to do this inline,
;                    ; but there might be one that I'm missing
-    FOR k=0,n_elements(tmp)-1 DO tmp[k]=(2^(i+1))*(3^(j+1))+k
-    bar.(i).(j)=tmp
-   endfor
- endfor
- end
% Compiled module: $MAIN$.
IDL>  print,bar
{
  a = {       6          18      54.0000}
      {       7          19      55.0000}
      {       8          20      56.0000}

  b = {      12          36      108.000}
      {      13          37      109.000}
      {      14          38      110.000}
      {      15          39      111.000}

}


whd

-- 
William Daffer: 818-354-0161: William.Daffer@jpl.nasa.gov