EBSILON®Professional Online Documentation
In This Topic
    Arrays
    In This Topic

    Arrays

    Options for handling arrays:

    • Static assignment of values:
      Example: b:=[3,4,6,9];
    • Direct assignment (a and b are arrays):
      Example: a:=b;
    • Test for equality (a and b are arrays):
      Example: if (a=b) then ...
    • Arrays (and also records) can be used in functions as return type.
    • In function calls arrays can be forwarded ”by value“. Then a complete copy of the array is pushed onto the stack.
      Arrays can also be forwarded ”by reference“ (use keyword var).

      Example:

      procedure modify (var s_arr: array[13..17] of integer);
      begin
           s_arr[15]:= 151515;
      end;
      procedure try_modify (s_arr: array[13..17] of integer);
      begin
           s_arr[15]:= 151515;
      end;

      procedure PrintSA (s_arr: array[13..17] of integer);
      var k:integer;
      begin
          print ("Integer array of length ", length (s_arr), ": ");
          for k := 13 to 17 do
             print (k,":", s_arr[k], ", ");
          print ("\n");
      end;

      var s_arr: array [13..17] of integer;
      begin
          s_arr[13] := 13;
          s_arr[14] :=-14;
          s_arr[15] := 15;
          s_arr[16] :=-16;
          s_arr[17] := 17;
          try_modify (s_arr);
          PrintSA (s_arr);
          modify (s_arr);
          PrintSA (s_arr);
      end.
    Integer array of length 5: 13:13, 14:-14, 15:15, 16:-16, 17:17
    Integer array of length 5: 13:13, 14:-14, 15:151515, 16:-16, 17:17

    There are three types of arrays:

     

    For static arrays there are the functions:

    Name

    Purpose

    Arguments

    Return value

    Example

    high

    yields the index of the last array element  

    if array is multi dimensional, the first index is used

    1: array

    INTEGER

    i:=high (array);

     

    length

    yields the length of the array

    1: array

    INTEGER

    a:array [1..10] of array [2..5] of integer;
    b:array [1..10, 2..5] of integer;

    e. g.  length(a)==length(b)==10

    but length(a[1])==length(b[1])==4

    low

    yields the index of the first array element

    1: array

    INTEGER

    i:=low (array);

    For dynamic arrays there are the functions:

    Name

    Purpose

    Arguments

    Return value

    Example

    append

    appends an array or an array element to another array
    Release 11: now allows to also append an individual element

     

    1: array of: Dest=dyn. array
    2: array or array element: Source

    None

     

    capacity

    yields the maximal number of elements that a dynamic array can obtain without automatic memory allocation (does not necessary match the array length)

    1: array: Source

    INTEGER

     i:=capacity(arObjects);

    copy

    copies an array onto another one

    1: array: Source

    array of: Dest

     

    high

    yields the index of the last array element  

    1: array of

    INTEGER

    i:=high (arObjects);

    insert

    inserts an array or an array element into another array at a certain position
    Release 11: now allows to also insert an individual element

    1: array of: Dest=dyn. array
    2: array or array element: Source
    3: INTEGER: Position

    None

     

    length

    yields the length of the array

    1: array of

    INTEGER

     

    low

    yields the index of the first array element

    1: array of

    INTEGER

    i:=low (arObjects);

    remove

    removes certain elements from an array

    1: array of: dyn. array
    2: INTEGER: Start Position
    3: INTEGER: Number of Elements

    None

     

    reserve

    allocates the storage space for a certain number of elements in an array (no elements are created or deleted, only the necessary storage is reserved)

    1: array of: dyn. array
    2: INTEGER: number of Elements

    None

     reserve(arObjects,100);

    setLength

    sets the length of the array to a new value  

    1: array of: dyn. array
    2: INTEGER: new length

    None

    var dyn_arr: array of real;
    setLength (dyn_arr, 2);
    dyn_arr[0]:= -45.7;
    dyn_arr[1]:=  5.7;

    reserve

    Release 11 allows minimum memory reservation for a given number of elements in an array (Please note: no elements are created or deleted, but only the required memory is reserved)  reserve does NOT replace the call to setLength) - this function can be used for long-term Optimization can be used to unnecessary Avoid reallocations of the array memory                 

    1: dyn. array

    2: INTEGER: Number of items to reserve

     

    var arr: array of string;
             i: integer;
    begin
             reserve( arr, 2000000);      
             println(capacity(arr));
             for i:=0 to 1999999 do

             begin
             setLength(arr, i+1);
             arr[i] := string(i);
             end;

             println("finished");

    end.  

     

    capacity

    Release 11 returns the maximum number of elements the array can contain without automatically creating new memory

     1: dyn. array

     

     See reserve

     

    For open arrays there are the following functions:

    Name

    Purpose

    Arguments

    Return value

    Example

    setLow

    Sets the index of the first element

    1: array * of
    2: INTEGER: new Index

    array * of

    procedure PrintCA (a: array * of char);
    var k:integer;
    begin
        print ("Char array of length ", length (a), ": ");
        for k := low  (a) to high (a) do
           print (k,":", a[k], ", ");
       print ("\n");
    end;

    var dyn_c: array of char;
    begin 
       setLength (dyn_c, 5);
       dyn_c[0]:= 'x';  dyn_c[1]:= '真';
       dyn_c[2]:= 'ü';   dyn_c[3]:= '仿';
       dyn_c[4]:= 'ê';

        PrintCA (dyn_c);
        PrintCA (setlow (dyn_c,-10));
       PrintCA (sliceLeft (dyn_c,3));
       PrintCA (sliceRight(dyn_c,2));
       PrintCA (sliceMid  (dyn_c,1,4));
    end.

    sliceLeft

    yields the n first elements of an array  

    1: array
    2: INTEGER: Number of Elements

    array * of

    sliceMid

    yields the n elements of an array from a certain position onwards

    1: array
    2: INTEGER: First Index
    3: INTEGER: Number of Elements

    array * of

    sliceRight

    yields the n last elements of an array  

    1: array
    2: INTEGER: Number of Elements

    array * of