tomatoeshift
tomatoeshift

Reputation: 485

In structured text: is it possible to write to a multi dimensional array with a single line in the cyclic code?

The following works fine:

PROGRAM PLC_PRG:
    VAR
        MyArray : ARRAY[0..1,0..5]OF USINT := [1,2,3,4,5,6,7,8,9,10,11,12];
        i : INT;
        j : INT;
    END_VAR

    // change to random values
    FOR i:=0 TO 1 DO
        FOR j:=0 TO 5 DO
            MyArray[i,j] := i+j;
        END_FOR
    END_FOR

    // Or individualy set numbers
    MyArray[0,1] := 56;
    MyArray[0,4] := 156;
END_PROGRAM

But what if I would like to modify all values in a single line of code?

i.e. the following is pseudo code of what I would like to do. (Note, it does not actually work)

PROGRAM PLC_PRG:
    VAR
        MyArray : ARRAY[0..1,0..5]OF USINT := [1,2,3,4,5,6,7,8,9,10,11,12];
        bChange : BOOL;
    END_VAR

    IF bChange THEN
        MyArray := [1,58,3,53,5,6,128,8,9,10,20,12];
    END_IF
END_PROGRAM

Upvotes: 2

Views: 3220

Answers (2)

Sergey Romanov
Sergey Romanov

Reputation: 3080

Only during initialization.

PROGRAM PLC_PRG:
    VAR
        MyArray : ARRAY[0..1,0..5]OF USINT := [
            [1,2,3,4,5,6],
            [1,2,3,4,5,6]
        ];
    END_VAR
END_PROGRAM

This will not be permitted

IF bChange THEN
    (* еhis will fial *)
    MyArray := [
            [1,2,3,4,5,6],
            [1,2,3,4,5,6]
        ];
END_IF

Upvotes: 0

Filippo Boido
Filippo Boido

Reputation: 1206

You need to program a function in order to simplify the assignment of the array-values.

Function declaration:

FUNCTION F_SetArrayValues : ARRAY[0..1,0..5]OF USINT
VAR_INPUT
     v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12 : USINT;    
END_VAR

Function implementation:

F_SetArrayValues[0,0] := v1;
F_SetArrayValues[0,1] := v2;
F_SetArrayValues[0,2] := v3;
F_SetArrayValues[0,3] := v4;
F_SetArrayValues[0,4] := v5;
F_SetArrayValues[0,5] := v6;
F_SetArrayValues[1,0] := v7;
F_SetArrayValues[1,1] := v8;
F_SetArrayValues[1,2] := v9;
F_SetArrayValues[1,3] := v10;
F_SetArrayValues[1,4] := v11;
F_SetArrayValues[1,5] := v12;

You call the function like this:

IF bChange THEN
    MyArray := F_SetArrayValues(2,3,4,5,6,7,8,9,10,11,12,13);
END_IF

Upvotes: 1

Related Questions