Language > Arrays > Concatenating Arrays

# Concatenating Arrays

To concatenate two arrays, create a new array with the arrays to be combined as elements:

array1 = FINDGEN(1000)

array2 = FINDGEN(999)

array3 = [array1, array2]

HELP, array3

IDL prints:

ARRAY3 FLOAT = Array[1999]

You can, of course, assign the value of the concatenation to one of the original arrays:

array1 = [array1, array2]

HELP, array1

IDL prints:

ARRAY1 FLOAT = Array[1999]

The arrays to be concatenated must be of the same dimensionality — that is, they must have the same number of dimensions, even if the sizes of the dimensions are different. In the example above, both `array1` and `array2` have one dimension, but are different sizes.

One common programming task is to build an array at runtime. For example, suppose you want to create an array that contains only the elements from an existing array that meet some test. Your code might look something like this:

new_array = [ ]

FOREACH element, existing_array DO BEGIN

IF my_test(element) THEN new_array = [new_array, element]

ENDFOREACH

Here, we initialize `new_array` as a null value first, so that the first element of `existing_array` that passes the test becomes the first element of `new_array`. See Null Values in Arrays for additional information on null arrays.