ARRAY-AND-MAP 1g 2023-11-23 laplante@plcb.ca GOWEB/ARRAY

title: “ARRAY-AND-MAP” author: “laplante@plcb.ca” date: 2023-11-23 version: “1.0.0”

section: “GOWEB/ARRAY”

Description

Arrays are used to create tables. Elements in an array can be of any type:

Maps are associative data types. Elements in a map can be of any type.
Note: There is no guaranteed order in a map.

Maps are created using:

map := { expression : expression [, expression : expression ] };

Before you can assign to a map using map.key,
the map must already be declared as in:

map := {};
map.key = value

Elements within an array can be accessed with the expression:

arr[expression]
or
arr[expression, expression...] for multi-dimensional arrays

Elements within a map can be accessed with the expression:

map.element
or
map["element"]

Index Range

A subpart of an array can be accessed using:

arr[index1:index2]

Index ranges can also be used for assignment, as in:

arr[4:] = "value";

Arrays and maps are passed by reference to functions.
For example:

res={{
    func f(b) {
        b.z = "zzzzz";
    }
    a := { "x" : 1, "y" : 2 };
    a;
    f(a);
    a;
}}.

return

res={"x":1,"y":2}{"x":1,"y":2,"z":"zzzzz"}.

When you assign a map or an array, a deep copy is performed.
For example:

res={{
    a := { "x" : 1, "y" : 2 };

    a;
    b := a;
    b.x = 7;
    a; "\n"; b;
}}.

return

res={"x":1,"y":2}{"x":1,"y":2}
{"x":7,"y":2}.

Examples

res={{
    x := { "x" : 1234, "y" : [1, { "z" : 1, "w" : 5678}]};
    x.y[1].w
}}.
res=5678.

res={{
    x := [1,2,3];
    x[1] = 7;
    x;
}}.
res=[1,7,3].

res={{
    x := { "x" :1, "y" :2, "z" : 3};
    x.y = 7;
    x;
}}.
res={"x":1,"y":7,"z":3}.

res={{
    x := { "x" :1, "y" : {"a" : 11, "b":22}, "z" : 4};
    x.y.b = 77;
    x;
}}.
res={"x":1,"y":{"a":11,"b":77},"z":4}.

res={{
    x := { "x" : 11, "y" : 22};
    x.x; x.y;
}}.
res=1122.

res={{
    x := { "x" : 11, "y" : 22};
    x["x"]; x["y"];
}}.
res=1122.

res={{
    x := { "x" : 11, "y" : 22};
    x.x = 33;
    x;
}}.
res={"x":33,"y":22}.

res={{
    x := { "x" : 11, "y" : {"z" : 22} };
    x.y.z;
    x.y.z = 33;
    x;
}}.
res=22{"x":11,"y":{"z":33}}.

res={{
    x := { "x" : 11, "y" : {"z" : {"w" : 22 } } };
    x.y.z.w;
    x.y.z.w=44;
    x.y.z.w;
    x;
}}.
res=2244{"x":11,"y":{"z":{"w":44}}}.

res={{
    x := { "x" : 11, "y" : {"z" : {"w" : 22 } } };
    x.y.z.w = 7;
    x;
}}.
res={"x":11,"y":{"z":{"w":7}}}.

res={{
    a := [1,2,[4,5,[7,8,9]]];
    a[2][2][1] = 88;
    "a="; a;
}}.
res=a=[1,2,[4,5,[7,88,9]]].

res={{
    a := [1,2,[4,5,6]];
    a[2][1] = 7;
    "a="; a;
}}.
res=a=[1,2,[4,7,6]].

res={{
    a := [1,2,3];
    x := 2;
    a[x] = a[x]*2;
    "a="; a;
}}.
res=a=[1,2,6].

res={{
    a := [ 1, { "x" : 2, "y" : [3, 4]}];
    a;
    a[0]=11;
    a[1].x=22;
    a[1].y[0]=33;
    a[1].y[1]=44;
    a;
}}.
res=[1,{"x":2,"y":[3,4]}][11,{"x":22,"y":[33,44]}].

res={{
    a := [ 1, { "x" : 2, "y" : [3, { "a" : 4, "b" : 5}]}];
    a;
    a[0]=11;
    a[1].x=22;
    a[1].y[0]=33;
    a[1].y[1].a=44;
    a[1].y[1].b=55;
    a;
}}.
res=[1,{"x":2,"y":[3,{"a":4,"b":5}]}][11,{"x":22,"y":[33,{"a":44,"b":55}]}].

res={{
    a :={ "w" : [1.1,2.2],"x" : 2, "y" : [3, { "a" : 4, "b" : 5}]};
    a;
    a.w[0] = 11;
    a.w[1]=22;
    a.x=222;
    a.y[0]=33;
    a.y[1].a=44;
    a.y[1].b=55;
    a;
}}.
res={"w":[1.1,2.2],"x":2,"y":[3,{"a":4,"b":5}]}{"w":[11,22],"x":222,"y":[33,{"a":44,"b":55}]}.

res={{
    a :={ "w" : [1.1,2.2],"x" : 2, "y" : [3, { "a" : 4, "b" : 5}]};
    for i :=0; i<3;i=i+1 {
        a; "\n";
        a.w[0] = 11+i;
        a.w[1]=22+i;
        a.x=222+i;
        a.y[0]=33+i;
        a.y[1].a=44+i;
        a.y[1].b=55+i;
        a; "\n";
    }
}}.
res={"w":[1.1,2.2],"x":2,"y":[3,{"a":4,"b":5}]}
{"w":[11,22],"x":222,"y":[33,{"a":44,"b":55}]}
{"w":[11,22],"x":222,"y":[33,{"a":44,"b":55}]}
{"w":[12,23],"x":223,"y":[34,{"a":45,"b":56}]}
{"w":[12,23],"x":223,"y":[34,{"a":45,"b":56}]}
{"w":[13,24],"x":224,"y":[35,{"a":46,"b":57}]}
.

res={{
    a := [1, 2.1, "string", true, false, null, [1,2,3], {"x":1,"y":2.2}];
    a;
}}.
res=[1,2.1,"string",true,false,null,[1,2,3],{"x":1,"y":2.2}].

See also