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
  • dos
  • tre

with "uno" actually storing the data.

Retrieving a value

This process decompiles the lists and retrieves one item, using the three indexes provided.

defineretrieveitemasep1bsep2cdeleteallofdosdeletealloftresetito0repeatlengthofitemaofunosetrtorepeatuntilletteriofitemaofuno=sep1changeiby1setrtojoinrletteriofitemaofunoendaddrtodosendsetito0repeatlengthofitembofdossetrtorepeatuntilletteriofitembofdos=sep2changeiby1setrtojoinrletteriofitembofdosendaddrtotreendsetrtoitemcoftre

The variable r is the value retrieved.

Storing a value

This process decompiles the lists, changes one value, then recompiles them.

definestoredatainasep1bsep2cdeleteallofdosdeletealloftresetito0repeatlengthofitemaofunosetrtorepeatuntilletteriofitemaofuno=sep1changeiby1setrtojoinrletteriofitemaofunoendaddrtodosendsetito0repeatlengthofitembofdossetrtorepeatuntilletteriofitembofdos=sep2changeiby1setrtojoinrletteriofitembofdosendaddrtotreendreplaceitemcoftrewithdatasetito0setrtorepeatlengthoftrechangeiby1setrtojoinjoinritemioftresep2endreplaceitembofdoswithrsetito0setrtorepeatlengthofdoschangeiby1setrtojoinjoinritemiofdossep1endreplaceitemaofunowithr