Chapter 12 Arrays

Table of Contents
12.1 Array Notation
12.2 Array Operators
12.3 Array Assignment
12.4 Array Element Assignment
12.5 Array Element Expansion
12.6 Inserting and Deleting Elements
12.7 Array Concatenation
12.8 Assignment and Concatenation
12.9 Joining Array Elements
12.10 Comparing Arrays
12.11 Array Length
12.12 Multidimensional Arrays
12.13 Undefining Arrays
12.14 Array How-to
12.15 Array Example

An array is a compound data type; it's a group of values, possibly of different data types.

Arrays can be very useful when used in conjunction with foreach loops, which allow to execute a statements block once for every element in a given array. Also, Cows' arrays can replace standard variables when you define some strings grouped by a logical relationship.

12.1 Array Notation

A literal array is indicated by a list of variables separated by commas and enclosed by braces:

{ 1, 2, 3 }
{ "Meat", "Is", "Murder" }
{ 1, "foo", 2, "bar" }

A variable storing an array can optionally be followed by braces like foo [ ] but it's not required. This notation can be useful to make your sources more readable since arrays are easily identified. Moreover, an error will be raised if you try to assign a scalar value to such variables. On the other hand, if you don't use braces, the same variable can refer to scalar values and arrays under different conditions. This adds flexibility to your code.

Important: notation doesn't affect the way variables are stored: foo and foo [ ] are exactly the same variable. You can also mix both notations:

foo [ ] = { 1, 2, 3 };
print (foo);
print (foo [ ]);

Array foo will be displayed twice:

{ 1, 2, 3 }
{ 1, 2, 3 }

All this can seem confusing at first, and probably differs from the notation used by your favorite scripting language (unless your favorite scripting language is G-Cows ...). In the following sections we'll always use the notation foo [ ] for the sake of clarity but remember that braces are optional.

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