This task is about arrays.

For hashes or associative arrays, please see Creating an Associative Array.

For a definition and in-depth discussion of what an array is, see Array.



Task

Show basic array syntax in your language.

Basically, create an array, assign a value to it, and retrieve an element   (if available, show both fixed-length arrays and
dynamic arrays, pushing a value into it).

Please discuss at Village Pump:   Arrays.

Please merge code in from these obsolete tasks:

   Creating an Array
   Assigning Values to an Array
   Retrieving an Element of an Array


Related tasks

   Collections
   Creating an Associative Array
   Two-dimensional array (runtime)


$ include "seed7_05.s7i";
 
const type: charArray is array [char] string;  # Define an array type for arrays with char index.
const type: twoDim is array array char;        # Define an array type for a two dimensional array.
 
const proc: main is func
  local
    var array integer: array1   is 10 times 0;           # Array with 10 elements of 0.
    var array boolean: array2   is [0 .. 4] times TRUE;  # Array with 5 elements of TRUE.
    var array integer: array3   is [] (1, 2, 3, 4);      # Array with the elements 1, 2, 3, 4.
    var array string: array4    is [] ("foo", "bar");    # Array with string elements.
    var array char: array5      is [0] ('a', 'b', 'c');  # Array with indices starting from 0.
    const array integer: array6 is [] (2, 3, 5, 7, 11);  # Array constant.
    var charArray: array7       is ['1'] ("one", "two"); # Array with char index starting from '1'.
    var twoDim: array8          is [] ([] ('a', 'b'),    # Define two dimensional array.
                                       [] ('A', 'B'));
  begin
    writeln(length(array1));    # Get array length (= number of array elements).
    writeln(length(array2));    # Writes 5, because array2 has 5 array elements.
    writeln(array4[2]);         # Get array element ("bar"). By default array indices start from 1.
    writeln(array5[1]);         # Writes b, because the indices of array5 start from 0.
    writeln(array7['2']);       # Writes two, because the indices of array7 start from '1'.
    writeln(array8[2][2]);      # Writes B, because both indices start from 1.
    writeln(minIdx(array7));    # Get minumum index of array ('1').
    array3[1] := 5;             # Replace element. Now array3 has the elements 5, 2, 3, 4.
    writeln(remove(array3, 3)); # Remove 3rd element. Now array3 has the elements 5, 2, 4.
    array1 := array6;           # Assign a whole array.
    array1 &:= [] (13, 17);     # Append an array.
    array1 &:= 19;              # Append an element.
    array1 := array3[2 ..];     # Assign a slice beginning with the second element.
    array1 := array3[.. 5];     # Assign a slice up to the fifth element.
    array1 := array3[3 .. 4];   # Assign a slice from the third to the fourth element.
    array1 := array3[2 len 4];  # Assign a slice of four elements beginning with the second element.
    array1 := array3 & array6;  # Concatenate two arrays and assign the result to array1.
  end func;