Skip to main content

2.9) Structures


Structures are useful for grouping related variables of different types together into one variable. They can be created using either curly brackets {} or the CREATE_STRUCT command. Here we will give an example of creating a structure using curly brackets. You can check the IDL help pages for how to use the CREATE_STRUCT command.

In this example, we will create a structure to hold the personal details (First name, surname, age, height) of a person:

elvis = {first_name:'Elvis', surname:'Presley', age:79, height:1.82}

The different fields (first_name, surname,age and height) are referred to as "tags". As you can see, to define the structure we start by opening the curly brackets. We then define the name of the tag, followed by a colon ":" and it's value. We continue creating tags and value pairs, each separated by a comma. When finished, close the brackets.

We now have a structure stored in the variable "elvis". In order to access the data held in the structure, we refer to the structure by it's variable name followed by a dot "." and the name of the tag for which we want the value. So to print the surname , we type:

print, elvis.surname

If you want to find out what variables are in a structure, use the help command.

help, elvis

In some older versions of IDL, you need to add ",/st" to the help command.

help, elvis, /st