Advanced Structure Usage
Facilities exist to process structures in a general way using tag numbers rather than tag names. Tags may be referenced using their index, enclosed in parenthesis, as follows:
Variable_name . (Tag_index) ... ... ...
The tag index ranges from 0 to the number of fields minus 1.
The N_TAGS function returns the number of fields in a structure. The TAG_NAMES function returns a string array containing the names of each tag.
Example of Tag Indices
Using tag indices, and the above-mentioned functions, we specify a procedure which reads into a structure from the keyboard. The procedure prompts you with the type, structure, and tag name of each field within the structure:
; A procedure to read into a structure, S, from keyboard input.
PRO READ_STRUCTURE, S
; Get the names of the tags.
NAMES = TAG_NAMES(S)
; Loop for each field.
FOR I = 0, N_TAGS(S)-1 DO BEGIN
; Define var. A as same type and structure as ith field.
A = S.(I)
; Use INFO to print the attributes of the field.
INFO, S.(I)
; Prompt user with field's tag name and read into var. A.
READ, 'Enter value for field ', NAMES(I), ': ', A
; Store back into structure from A.
S.(I) = A
ENDFOR
END
Note, in the above procedure the READ procedure reads into the variable A rather than S.(I), because S.(I) is an expression, not a simple variable reference. Expressions are passed by value; variables are passed by reference. The READ procedure prompts you with parameters passed by value and reads into parameters passed by reference.