REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: array

array  size  /initial  value

Makes and initializes a series of a given size.

Arguments:

size [integer! block!] - Size or block of sizes for each dimension

Refinements:

/initial - Specify an initial value for all elements

value - Initial value (will be called each time if a function)

See also:

make   pick   poke  

Description

In REBOL, arrays are simply blocks that are initialized to a specific size with all elements set to an initial value (v:none by default). The array function is used to create and initialize arrays.

Supplying a single integer as the argument to array will create an array of a single dimension. The example below creates a five element array with values set to none:

block: array 5
probe block
[none none none none none]
print length? block
5

To initialize an array to values other than NONE, use the /initial refinement. The example below intializes a block with zero values:

block: array/initial 5 0
probe block
[0 0 0 0 0]

To create an array of multiple dimensions, provide a block of integers as the argument to the array function. Each integer specifies the size of that dimension of the array. (In REBOL, such multidimensional arrays are created using blocks of blocks.)

xy-block: array [2 3]
probe xy-block
[[none none none] [none none none]]
xy-block: array/initial [2 3] 0
probe xy-block
[[0 0 0] [0 0 0]]

Once an array has been created, you can use paths or the pick and poke functions to set and get values of the block based on their indices:

block/3: 1000
poke block 5 now
probe block
[0 0 1000 0 12-Feb-2009/17:46:59-8:00]
probe block/3
1000
repeat n 5 [poke block n n]
probe block
[1 2 3 4 5]
xy-block/2/1: 1.2.3
xy-block/1/3: copy "test"
probe xy-block
[[0 0 "test"] [1.2.3 0 0]]
probe xy-block/2/1
1.2.3
repeat y 2 [
    dim: pick xy-block y
    repeat x 3 [poke dim x to-pair reduce [x y]]
]
probe xy-block

Coding Style Notes

REBOL uses the concept of expandable series for holding and manipulating data, rather than the concept of fixed size arrays. For example, in REBOL you would normally write:

block: copy []
repeat n 5 [append block n]

rather than:

block: array 5
repeat n 5 [poke block n n]

In other words, REBOL does not require you to specify the size of data arrays (blocks, bytes, or strings) in advance. They are dynamic.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin