Multidimensional Arrays
Шаблон:Merge Шаблон:See also A multidimensional array is an array that contains other arrays of the same length; for example, an array with length 3 that contains 3 arrays, each of length 3. Currently, Scratch does not support such arrays; however, they can be replicated with strings. To do so, one compiles one list into a string (separated by a character such as a semicolon), adds that string as an item to a second list, compiles the second list into another string, and adds that string into a third list, then compiles that third list... and so on, for however many dimensions the array contains.
Tutorial
This tutorial assumes two variables:
(i)
(r)
It also assumes (assuming this is a 3-dimensional list) three lists:
(uno::list)
(dos::list)
(tre::list)
with "uno" actually storing the data.
Retrieving a value
This process decompiles the lists and retrieves one item, using the three indexes provided.
define retrieve item (a) (sep1) (b) (sep2) (c) delete (all v) of [dos v] delete (all v) of [tre v] set [i v] to [0] repeat (length of (item (a) of [uno v])) set [r v] to [ ] repeat until <(letter (i) of (item (a) of [uno v])) = (sep1)> change [i v] by (1) set [r v] to (join (r) (letter (i) of (item (a) of [uno v]))) end add (r) to [dos v] end set [i v] to [0] repeat (length of (item (b) of [dos v])) set [r v] to [ ] repeat until <(letter (i) of (item (b) of [dos v])) = (sep2)> change [i v] by (1) set [r v] to (join (r) (letter (i) of (item (b) of [dos v]))) end add (r) to [tre v] end set [r v] to (item (c) of [tre v])
The variable (r)
is the value retrieved.
Storing a value
This process decompiles the lists, changes one value, then recompiles them.
define store (data) in (a) (sep1) (b) (sep2) (c) delete (all v) of [dos v] delete (all v) of [tre v] set [i v] to [0] repeat (length of (item (a) of [uno v])) set [r v] to [ ] repeat until <(letter (i) of (item (a) of [uno v])) = (sep1)> change [i v] by (1) set [r v] to (join (r) (letter (i) of (item (a) of [uno v]))) end add (r) to [dos v] end set [i v] to [0] repeat (length of (item (b) of [dos v])) set [r v] to [ ] repeat until <(letter (i) of (item (b) of [dos v])) = (sep2)> change [i v] by (1) set [r v] to (join (r) (letter (i) of (item (b) of [dos v]))) end add (r) to [tre v] end replace item (c) of [tre v] with (data) set [i v] to [0] set [r v] to [ ] repeat (length of [tre v]) change [i v] by (1) set [r v] to (join (join (r) (item (i) of [tre v])) (sep2)) end replace item (b) of [dos v] with (r) set [i v] to [0] set [r v] to [ ] repeat (length of [dos v]) change [i v] by (1) set [r v] to (join (join (r) (item (i) of [dos v])) (sep1)) end replace item (a) of [uno v] with (r)