REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Blocks: Arrays

Pending Revision

This document was written for R2 and has yet to be revised for R3.

Blocks are used for arrays.

An example of a statically defined two dimensional array is:

arr: [
    [1   2   3  ]
    [a   b   c  ]
    [$10 $20 $30]

You can obtain the values of an array with the series extraction functions:

probe first arr
[1 2 3]
probe pick arr 3
[$10.00 $20.00 $30.00]
probe first first arr

You can also use paths to obtain values from the array:

probe arr/1
[1 2 3]
probe arr/3
[$10.00 $20.00 $30.00]
probe arr/3/2

Paths can also be used to change the values in an array:

arr/1/2: 20
probe arr/1
[1 20 3]
arr/3/2: arr/3/1 + arr/3/3
probe arr/3/2

Creating Arrays

The array function creates arrays dynamically. The function takes an argument that is either an integer or a block of integers and returns a block that is the array. By default, the cells of an array are initialized to none!. To initialize array cells to some other value, use the /initial refinement explained in the next section.

When array is supplied with a single integer, a one-dimensional array of that size is returned:

arr: array 5
probe arr
[none none none none none]

When a block of integers is provided, the array has multiple dimensions. Each integer provides the size of the corresponding dimension.

Here is an example of a two dimensional array that has six cells, two rows of three columns:

arr: array [2 3]
probe arr
[[none none none] [none none none]]

This can be made into a three dimensional array by adding another integer to the block:

arr: array [2 3 2]
foreach lst arr [probe lst]
[[none none] [none none] [none none]]
[[none none] [none none] [none none]]

The block of integers that is passed to array can be as big as your memory will support.

Initial Values

To initialize the cells of an array to a value other than none!, use the /initial refinement. This refinement takes one argument: the initial value. Here are some examples:

arr: array/initial 5 0
probe arr
[0 0 0 0 0]
arr: array/initial [2 3] 0
probe arr
[[0 0 0] [0 0 0]]
arr: array/initial 3 "a"
probe arr
["a" "a" "a"]
arr: array/initial [3 2] 'word
probe arr
[[word word] [word word] [word word]]
arr: array/initial [3 2 1] 11:11
probe arr
[[[11:11] [11:11]] [[11:11] [11:11]] [[11:11] [11:11]]]

  TOC < Back Next > - WIP Wiki Feedback Admin