Home   Help Search Login Register  

Author Topic: Parsing/setting multi-dimensional arrays [SOLVED]  (Read 3130 times)

0 Members and 1 Guest are viewing this topic.

Offline indianz

  • Members
  • *
Parsing/setting multi-dimensional arrays [SOLVED]
« on: 17 Mar 2011, 00:23:30 »
Trying to set values in an array in an array (one array with 4x4 indices), but can't figure out the correct way or syntax for it (if it is, indeed, even possible). Using a dirty quick-fix for it now, but am far from satisfied. Here it is:

_temp = (mainarray select _x);
_temp set [_y, _temp];
mainarray set [_x, _temp];

Anyone?
« Last Edit: 19 Mar 2011, 22:29:52 by indianz »

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: Parsing/setting multi-dimensional arrays
« Reply #1 on: 17 Mar 2011, 10:25:01 »
Heya!

Arrays-within-arrays has always been a complicated matter in the .sqf engine; it works, but to change stuff in it you usually have to rebuild the whole array from scratch (reading is easier). :no: Basically it gets dirty no matter how you do it.

However, I'm pretty sure Arma 2's function library contains a host of functions for array building and editing and whatnot. Here's the BIKI page [Link] for how to start up the full list of functions in-game - there's a ton of array editing functions there you can probably use (just see the screenshot there).

If you want specifics, do keep asking, but as mentioned: lots of stuff there to get you started.

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline indianz

  • Members
  • *
Re: Parsing/setting multi-dimensional arrays
« Reply #2 on: 19 Mar 2011, 22:29:33 »
Excellent! There's a load of array-functions. For future reference, it was BIS_fnc_setNestedElement I was looking for.

Thank you Wolfrug, appreciate it :)

Offline Denisko-Redisko

  • Members
  • *
Re: Parsing/setting multi-dimensional arrays [SOLVED]
« Reply #3 on: 24 Mar 2011, 18:36:14 »
2indianz
Code: [Select]
_temp set [_y, _temp]; // this code creates circular referencesome example:
Code: [Select]
matrix = [
    ["a1", "a2", "a3", "a4"],
    ["b1", "b2", "<>", "b4"],
    ["c1", "c2", "c3", "c4"],
    ["d1", "d2", "d3", "d4"]
];

matrix select 1 set [2, "B3"]

Code: [Select]
matrix = [
    [
        ["1.a1", "1.a2", "1.a3", "1.a4"],
        ["1.b1", "1.b2", "<  >", "1.b4"],
        ["1.c1", "1.c2", "1.c3", "1.c4"],
        ["1.d1", "1.d2", "1.d3", "1.d4"]
    ],
    [
        ["2.a1", "2.a2", "2.a3", "2.a4"],
        ["2.b1", "2.b2", "2.b3", "2.b4"],
        ["2.c1", "<  >", "2.c3", "2.c4"],
        ["2.d1", "2.d2", "2.d3", "2.d4"]
    ],
    [
        ["3.a1", "3.a2", "3.a3", "3.a4"],
        ["3.b1", "3.b2", "3.b3", "3.b4"],
        ["3.c1", "3.c2", "3.c3", "----"],
        ["3.d1", "3.d2", "3.d3", "3.d4"]
    ],
    [
        ["4.a1", "4.a2", "4.a3", "4.a4"],
        ["4.b1", "4.b2", "4.b3", "4.b4"],
        ["4.c1", "4.c2", "4.c3", "4.c4"],
        ["<  >", "4.d2", "4.d3", "4.d4"]
    ]
];

matrix select 0 select 1 set [2, "1.B3"];
matrix select 1 select 2 set [1, "2.C2"];
matrix select 2 select 2 set [3, "3.C4"];
matrix select 3 select 3 set [0, "4.D1"];

_row = matrix select 3 select 3;
for "_i" from 0 to count _row -1 do {
    _row set [_i, toUpper (_row select _i)]
};

matrix call compile preprocessFileLineNumbers "showArray.sqf"

for debugging use this function:
Code: [Select]
// showArray.sqf
private ["_result", "_indents", "_depth", "_0D0A", "_getIndent", "_writeLine", "_unwrapTree"];
_result = "";
_indents = [""];
_depth = 0;
_0D0A = toString [13,10];
_getIndent = {
    if (_depth >= count _indents) then {
        _indents set [_depth, (_indents select _depth-1) + "    "];
    };
    _indents select _depth;
};
_writeLine = {
    _result = _result + _0D0A + (call _getIndent) + _this;
};
_unwrapTree = {
    switch (typeName _this) do {
        case "ARRAY": {
            "[" call _writeLine;
            _depth = _depth + 1;
            { _x call _unwrapTree } foreach _this;
            _depth = _depth - 1;
            "]," call _writeLine;
        };
        default {
            str _this + "," call _writeLine
        };
    };
};
_this call _unwrapTree;
copyToClipboard _result;
_result;

sorry for my english