REBOL Document

Array - Function Summary


Summary:

Makes and initializes a series of a given size.

Usage:

array size

Arguments:

size - Size or block of sizes for each dimension (must be: integer block)

Refinements:

/initial - Specify an initial value for all elements

value - Initial value

Description:

In REBOL, arrays are simply blocks that are initialized to a specific size with all elements set to an initial value (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 9-Mar-2004/0:59:44-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
    [[1x1 2x1 3x1] [1x2 2x2 3x2]]

Coding style note: 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.

Related:

make - Constructs and returns a new value.
pick - Returns the value at the specified position in a series.
poke - Returns value after changing its data at the given index. (See manual)


<Back | Index | Next>

Copyright 2004 REBOL Technologies