Project

General

Profile

Actions

Array in Scol

Arrays are helpful. Like any language, an array is a collection of elements, each identifed by an index. Its size is fixed. The access time of any element is always the same. The data type should be unchanged (like C but not like Python).
An array can be one dimensional or multi dimensional.

Note : in Scol, a string (type S) is not an array.

What is the array type ?

This type is tab u0
u0 can be any type, simple or complex. For example :
tab S : an array of strings
tab [I F] : an array of tuples
tab [S r1] : an array of lists
tab [S I tab S] : an array of tuples with a sub array

How create an array ?

By the Scol function mktab : mktab <initial_value> <size>

Example :

typeof array = tab I;;

fun main ()=
    _showconsole;
    set array = mktab 1 10;    // size = 10, each element at 1
    0;;

How to set an element ?

By its index :

typeof array = tab S;;

fun main ()=
    _showconsole;
    set array = mktab nil 5;
    set array.0 = "Bob";
    set array.1 = "Alice";
    set array.2 = "Uma";
    set array.3 = "Kevin";
    set array.4 = "James";
    0;;

or, depending on the context :


typeof array = tab S;;

fun setArray (list, index)=
    if list == nil then
        0    // done
    else
    (
        set array.index = hd list;
        setArray tl list index+1
    );;

fun main ()=
    _showconsole;
    set array = mktab nil 5;
    setArray "Bob"::"Alice"::"Uma"::"Kevin"::"Jason"::nil 0;
    0;;

For a multidimensioanl array :

set array[indexA][indexB] = value;

How to get an element ?

By its index :

set myVariable = array[index];

How get the size of an array ?

By the Scol function tabsize : tabsize <array> or by a recursive function (see List in Scol).

How to create an associative array ?

By a tuple with two elements.
For example :


typeof array = tab [S I];;

fun main ()=
    _showconsole;
    set array = tab nil 128;
    set array.0 = ["Bob" 75];
    set array.1 = ["Alice" 25];
    ...
    0;;

- Others

The non standard library Syspack provides others array functions.

License : CC-BY-SA-2.0
Tutorial by iri
Updated by /

Updated by iri about 12 years ago · 1 revisions