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.
Number of Structure Tags
The function N_TAGS(Structure) returns the number of fields in a structure. To obtain the size, in bytes, of a structure call N_TAGS with the /LENGTH keyword.
Names of Structure Tags
The function TAG_NAMES(Structure) returns a string array containing the names of each tag. To return the name of the structure itself, call TAG_NAMES with the /STRUCTURE_NAME keyword.
Access Fields by Tag Number
A tag can be referenced using its index rather than its tag name. The tag index should be enclosed in parentheses, as follows:
Variable_Name.(Tag_Index)... ... ...
The Tag_Index ranges from zero to the number of fields minus one.
Note: The Tag_Index is an expression, the result of which is taken to be a tag position. In order for the IDL parser to understand that this is the case, you must enclose the Tag_Index in parentheses. This is not an array indexing operation, so using square brackets [ ] is not allowed in this context.
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, *]
Storing Into Array Fields
The semantics of storing into structure array fields is slightly different than storing into simple arrays. The main difference is that with structures, a subscript range must be used when storing an array into part of an array field. With normal arrays, when storing an array inside part of another array, use the subscript of the lower-left corner, not a range specification. Other differences occur because the size and type of a field are fixed by the original structure definition, and the normal semantics of dynamic binding are not applicable. The rules for storing into array fields are as follows:
VAR.ARRAY_TAG = Scalar_Expression
All elements of VAR.tag are set to Scalar_Expression. For example:
A.inten = 100
VAR.TAG = Array_Expression
Each element of Array_Expression is copied into the array VAR.tag. If Array_Expression contains more elements than the destination array does, an error results. If it contains fewer elements than VAR.TAG, the unmatched elements remain unchanged. For example:
A.inten = FINDGEN(12)
A.inten = [1, 2]
VAR.TAG[Subscript] = Scalar_Expression
The value of the scalar expression is copied into the designated element of the destination. If Subscript is an array of subscripts, the scalar expression is copied into the designated elements. For example:
A.inten[5] = 100
A.inten[[2, 4, 6]] = 100
VAR.TAG[Subscript] = Array_Expression
Unless VAR.tag is an array of structures, the subscript must be an array. Each element of Array_Expression is copied into the element given by the corresponding element subscript. For example:
A.inten[[2, 4, 6]] = [5, 7, 9]
VAR.TAG[Subscript_Range] = Scalar_Expression
The value of the scalar expression is stored into each element specified by the subscript range. For example:
A.inten[8:*] = 5
VAR.TAG[Subscript_Range] = Array_Expression
Each element of the array expression is stored into the element designated by the subscript range. The number of elements in the array expression must agree with the size of the subscript range. For example:
A.inten[3:6] = FINDGEN(4)
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:
A = {star, name:'SIRIUS', ra:30., dec:40., inten:INDGEN(12)}
A.name = 'BETELGEUSE'
PRINT, A.name, A.ra, A.dec
Q = A.inten[5]
A.ra = 23.21
A.inten = 0
B = A.inten[3:6]
A.name = 12
B = A