Programmer Guide > Working with Structures > Creating Arrays of Structures
  

Creating Arrays of Structures
An array of structures is simply an array in which each element is a structure of the same type. The referencing and subscripting of these arrays (also called structure arrays) follow essentially the same rules as simple arrays.
The easiest way to create an array of structures is to use the REPLICATE function. The first parameter to REPLICATE is a reference to the structure of each element. Using the above example of a star catalog and assuming the CATALOG structure has been defined, an array which contains 100 elements of the structure is created with the statement:
CAT = REPLICATE({ CATALOG }, 100)
Alternatively, since the variable STAR contains an instance of the structure CATALOG:
CAT = REPLICATE(STAR, 100)
Or, to define the structure and an array of the structure in one step:
CAT = REPLICATE({ CATALOG, NAME : '', RA: 0.0, $
DEC : 0.0, INTEN : FLTARR(12) }, 100)
The concepts and combinations of subscripts, subscript arrays, subscript ranges, fields, nested structures, etc., are general and lead to many possibilities, only a small number of which can be explained here. In general what seems reasonable usually works.
Examples of Arrays of Structures
Using the above definition in which the variable CAT contains a star catalog of CATALOG structures:
; Set the NAME field of all 100 elements to EMPTY.
CAT.NAME = 'EMPTY'
; Set CAT's ith element to the contents of the CATALOG structure.
CAT(I) = {CATALOG, 'BETELGEUSE', 12.4, 54.2, FLTARR(12)}
; Store 0.0 in CAT(0).RA, 1.0 in CAT(1).RA, ..., 99.0 in 
; CAT(99).RA.
CAT.RA = INDGEN(100)
; Prints name field of all elements of CAT, separated by commas.
PRINT, CAT.NAME + ','
; Find index of star with name of SlRIUS.
I = WHERE(CAT.NAME EQ 'SIRIUS')
; Extract intensity field from each entry. Q will be a 12-by-100
; floating point array.
Q = CAT.INTEN
; Plot intensity of 6th star in array CAT.
PLOT, CAT(5).INTEN
; Make contour plot of the (7, 46) floating-point array taken from
; months (2:8) and stars (5:50).
CONTOUR, CAT(5 : 50).INTEN(2:8) 
; Sort the array into ascending order by names. Store the result
; back into CAT.
CAT = CAT(SORT(CAT.NAME))
; Determine monthly total intensity of all stars in array.
; MONTHLY is now a 12-element array.
MONTHLY = CAT.INTEN # REPLICATE(1,100)

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.