Previous Application Programming: Structures Next

Structure References

The basic syntax of a reference to a field within a structure is as follows:

Variable_Name.Tag_Name

Variable_Name must be a variable that contains a structure. Tag_Name is the name of the field and must exist in the structure. If the field referred to by the tag name is itself a structure, the Tag_Name can optionally be followed by one or more additional tag names, as shown by the following example:

var.tag1.tag2  

Each tag name, except possibly the last, must refer to a field that contains a structure.

Subscripted Structure References

A subscript specification can be appended to the variable or tag names if the variable is an array of structures or if the field referred to by the tag contains an array. Scalar fields within a structure can also be subscripted, provided the subscript is zero.

Variable_Name.Tag_Name[Subscripts]

Variable_Name[Subscripts].Tag_Name...

Variable_Name[Subscripts].Tag_Name[Subscripts]

Each subscript is applied to the variable or tag name it immediately follows. The syntax and meaning of the subscript specification is similar to simple array subscripting in that it can contain a simple subscript, an array of subscripts, or a subscript range. If a variable or field containing an array is referenced without a subscript specification, all elements of the item are affected. Similarly, when a variable that contains an array of structures is referenced without a subscript but with a tag name, the designated field in all array elements is affected. The complete syntax of references to structures follows. (Optional items are enclosed in braces, {}.)

Structure_reference:= Variable_Name{[Subscripts]}.Tags

Tags:= {Tags.}Tag

Tag:= Tag_Name{[Subscripts]}

For example, all of the following are valid structure references:

A.B  
A.B[N, M]  
A[12].B  
A[3:5].B[*, N]  
A[12].B.C[X, *]  

The semantics of storing into a structure field using subscript ranges is slightly different than that of simple arrays. This is because the structure of arrays in fields are fixed. See Storing Into Array Fields for details.

Examples of Structure References

The name of the star contained in A is referenced as A.NAME. The entire intensity array is referred to as A.INTEN, while the n-th element of A.INTEN is A.INTEN[N]. The following are valid IDL statements using the STAR structure:

;Store a structure of type STAR into variable A. Define the values   
;of all fields.  
A = {star, name:'SIRIUS', ra:30., dec:40., inten:INDGEN(12)}  
  
;Set name field. Other fields remain unchanged.  
A.name = 'BETELGEUSE'  
  
;Print name, right ascension, and declination.  
PRINT, A.name, A.ra, A.dec  
  
;Set Q to the value of the sixth element of A.inten. Q will be a   
;floating-point scalar.  
Q = A.inten[5]  
  
;Set ra field to 23.21.  
A.ra = 23.21  
  
;Zero all 12 elements of intensity field. Because the type and size   
;of A.inten are fixed by the structure definition, the semantics of   
;assignment statements is different than with normal variables.  
A.inten = 0  
  
;Store fourth thru seventh elements of inten field in variable B.  
B = A.inten[3:6]  
  
;The integer 12 is converted to string and stored in the name field   
;because the field is defined as a string.  
A.name = 12  
  
;Copy A to B. The entire structure is copied and B contains a STAR   
;structure.  
B = A  

  IDL Online Help (March 06, 2007)