Wikipedia-logo.svg  For more information, see Array data structure on Wikipedia. An array is an ordered collection of values. It is similar to a list, however, most high-level languages provide first-class data which allows the concept of "an array of arrays" to be feasible. An array has numbers numbering every item, usually sequential integers. Arrays in arrays are known as nested arrays. If each nested array has the same length, the array is given a dimension, or depth, which is the number of nested arrays. A list is, therefore, a 1 dimensional array as there is one array, and no members of it are arrays themselves. When speaking about arrays, the term index indicates an item number. In most text-based languages, indices begin with zero rather than one.

For example, here is a 3-dimensional array ([a, b, c] indicates an array with objects a, b, and c.)

  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]
  [x, x, x], [x, x, x], [x, x, x]

Here, "x" is an atom, or a single non-array value, like a number or string. It can be considered as a 0-dimensional array.

Arrays are usually not mutable. Mutability is the ability to add and remove items individually. Most languages do not allow arrays to be mutated. However, when treating arrays as lists of lists or stacks of lists, one can mutate them. Thus BYOB's concept of an array allows greater functionality.

Visualization of an array

An array can be visualized in many ways. One way is as follows:

  • An atom (0 dimensions) is represented as a node or vertex.
  • A 1-dimensional list is a set of some points. Many points together in a line form a list.
  • A 2-dimensional array is a list of lines, or it is a set of lines. Many lines stacked upon each other form a square or a rectangle.
  • A 3-dimensional array is formed by stacking squares on each other. This forms a cube.
  • A 4-dimensional array cannot be visualized in its perfect form as seen in other examples above, but a 4-dimensional array can be partially visualized as cubes put inside each other, like boxes with boxes in them. This is called a hypercube, or tesseract.

An array can also be visualized as a tree. Each node is a branch, and array nodes give way to more branches.

Similar data types


Lists are similar to one-dimensional arrays. However, they are always mutable. Since they are always one-dimensional, they have many special constructs such as the pair construct and the stack construct, unlike arrays who are always constructed with a simple integer-value construct.

Pair Construct

The pair construct basically assumes a list to be a pair of two items: the first item of the list and the rest of the list. This idea is useful in functional languages like Scheme, where recursion is common. Here, complex tasks can be applied to lists with the logic that a function of a list can be written as a function of the first item of the list and the function of the rest of the list.

Paired lists have three major commands:

  • cons: This adds an item to the top of a list
  • car: This returns the first item of the list
  • cdr: This returns the rest of the list

It is interesting to see how an array can be used to simulate a pair construct: an array with two items, where the first is always an atom and the second is always another array-pair. This would look like this:

 car of list,
 [car of cdr of list,
   car of cdr of cdr of list,
   cdr of cdr of cdr of list (etc.)

Stack Construct

A stack construct does not allow referencing of items in the list. It allows you to only add items and delete items. There are two commands here:

  • push: This adds an item to the top of a stack
  • pop: This deletes the top item and reports it

You are meant to push in values, then pop them off at a later stage to evaluate.

Arrays cannot simulate stacks. This is because arrays are usually non-mutable, and adding and deleting items needs mutability.

Lists have many advantages over arrays, especially as far as speed and performance go. Recursive programming is also easier and more elegant. See more about advantages of lists over arrays on this Wikipedia section.


Arrays are quite similar to Objects. However, the key difference is that arrays have numerical keys while Objects have string keys.

Multidimensional Arrays in Scratch

Main article: Multidimensional Arrays


How to store the information.

Currently, Scratch does not support multidimensional arrays. There are ways, however, to simulate them using lists.

Method 1

In method 1, the list is used just like a two-dimensional array, where each item in the list is a row of the array, and the letter of the item represents the column number in the array. It is important to note that in this method, only single character values can be stored.

Access the data with this script:

set [Value v] to (letter (X) of (item (Y) of [Array v]))

Method 2

How to store the information from this array: 2D Array Comment.png

In method 2, each item in the list is an item in the array. This allows for array items to contain multiple characters and the array to be of multiple dimensions.

The variable "Columns" should not be used; rather, the number of columns should be substituted.

set [Value v] to (item ((((Y) - (1)) * (Columns)) + (X)) of [Array v])

If "X" is set to 3, "Y" is set to 2, and "Columns" is set to 4, this should set "Value" to "2/3".


An example array the script is based on. Each comment is a sub-array.

This method is based on method 2 of a 2D array.

First, the 3D array must be constructed. This can be done with the following script:

set [z iterator v] to [1]
repeat (depth)
 set [y iterator v] to [1]
 repeat (columns)
  set [x iterator v] to [1]
  repeat (rows)
   add (join (z iterator) (join [/] (join (y iterator) (join [/] (x iterator) ) ) ) ) to [Array v]
   change [x iterator v] by (1)
  change [y iterator v] by (1)
 change [z iterator v] by (1)
Note Note: Scroll to see the entire script.

Setting "depth", "columns", and "rows" to 3 will produce the array shown on the side.

The following script will get information from the array:

set [value v] to (item ( ( ( (Z) - (1) ) * ( (depth) * (columns) ) ) + ( ( ( (Y) - (1) ) * (columns) ) + (X) ) ) of [Array v])

Multidimensional Arrays in BYOB

BYOB3.png This article or section uses images made with Snap!, a Scratch Modification which allows script formatting. Differences include block multilining and zebra coloring.
Creating a 3D array in BYOB with the first-class list block

In the Scratch Modification BYOB, you can create first-class lists. This allows users to create lists of lists, making multidimensional arrays easier. The list [] @delInput @addInput :: list reporter block allows you to generate and report a list with the inputs given; by placing a list as one of the arguments themselves you can make a list of lists.

Note Note: In the rest of this section, [a, b, c] will represent the list of a, b, and c; and list[n] will represent the nth item of "list". These are common shorthand for the (list [] < >) block and the (item () of (list)) block, respectively.
Note Note: Here, for simplicity of reading, in the expression a[n], n is not the index but the item number.

To access nested items in a BYOB multidimensional array, you would first access the nested list, then the item. For example:

array = [[1, 2, 3], [4, 5, 6]]

array[1]=[1, 2, 4]
array[2]=[4, 5, 6]

You need to initialize arrays before you fill them in:

array = [] (empty list)

array[1] = ERROR (not defined)

array = [[], [], []]

array[1][1]=ERROR (not defined)

It is a common mistake to attempt to access or change uninitialized array items.

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.