next up previous contents
Next: Structures Up: Variables, data types Previous: Variables, data types   Contents

arrays

In addition to single, scalar variables, IDL has arrays. An array is a numbered group of variables, all of the same type. We used arrays in the first lecture to hold the data for line plots. You can define an array by putting the elements between square brackets, like this:

IDL> an_array=[3, 5, 6, 2.5, 100, 27.7]

Note that we have mixed up floats and integers in this expression; IDL will coerce all the integers to floats if there are any floats at all. If you define an array using integers only then IDL will leave them as integers.

You can refer to individual elements of the array like this:

IDL> print,an_array[0],an_array[2]
      3.00000      6.00000
Note that the first element of the array is numbered 0 as in `C' (not 1 as in FORTRAN). Note also that you can use () or [] for indexing arrays. The square brackets are a recent addition to the language, but are the wiser choice because the parentheses () have other uses. You can refer to a subset of the array like this:
print,an_array[3:5]
      2.50000      100.000      27.7000

You can define a 2-dimensional array like this:

IDL> array_2d= [[ 2,3,4],[10,20,30]]; 3 columns , 2 rows

... and refer to parts of it like this:

IDL> print,array_2d[0,1]
      10
IDL> print,array_2d[1:2,1]
      20      30
IDL> print,array_2d[2,*]
       4
      30

Note how an asterisk, *, is used to represent an entire column or row of the array and how two integers and a colon are used to select part of a row or column.

Also, note

help,array_2d(0,*) ; is <Expression>    INT       = Array[1, 2]
This can cause problems when you want to operate on it as a vector (i.e. a truly 1d array). You can get around this using,
column_zero=reform(array_2d(0,*))
help,column_zero;COLUMN_ZERO     INT       = Array[2]

Finally, you can set up an array to use later like this:

IDL> foo=fltarr(100,100)
will make foo a 100 by 100 element floating point array with all elements initialised to 0.0 OR
IDL> foo=intarr(100,100); OR
IDL> foo=strarr(100,100); etc


next up previous contents
Next: Structures Up: Variables, data types Previous: Variables, data types   Contents
John Marsham 2005-04-22