12.7 Array Concatenation

Given two arrays, their concatenation is an array containing all the elements from the first array and all the elements from the second.

12.7.1 Synopsis

expr_1 + expr_2

12.7.2 Description

If both expressions are arrays, a new array containing all the elements from expr_1 and from expr_2 is returned.

If expr_1 is an array expression and expr_2 is a scalar one, a new array is returned. Its elements are all the elements from expr_1 followed by the scalar value expr_2.

If expr_1 is a scalar expression and expr_2 is an array, a new array is returned. Its first element is expr_1, followed by all the elements from expr_2.

12.7.3 Example

First, let's define two arrays reasons_1 [ ] and reasons_2 [ ] and their concatenation reasons [ ]. Array reasons [ ] will contain all elements from reasons_1 [ ] and all elements from reasons_2 [ ].

reasons_1 [ ] = {"for sport, ", "for pleasure, ", "for adventures, "};
reasons_2 [ ] = {"and for hides ", "and furs "};

reasons [ ] = reasons_1 [ ] + reasons_2 [ ];

Now, we define array quote [] by concatenating a scalar value, array reasons [ ] and a literal array.

quote [ ] = "Killing animals " + reasons [ ]
            + { "is a phenomenon which is at once",
                "disgusting and distressing. ",
                "There is no justification in indulging ",
                "in such acts of brutality." };

Last, we add a scalar expression (a literal string).

quote [ ] = quote [ ] + "-- The XIV Dalai Lama of Tibet";

These are the elements and their values:

quote [0]

Killing animals

quote [1]

for sport,

quote [2]

for pleasure,

quote [3]

for adventures,

quote [4]

and for hides

quote [5]

and furs

quote [6]

is a phenomenon which is at once

quote [7]

disgusting and distressing.

quote [8]

There is no justification in indulging

quote [9]

in such acts of brutality.

quote [10]

- The XIV Dalai Lama of Tibet

This manual can be downloaded from http://www.g-cows.org/.