| # Chapter 5: Building Arrays This chapter is about building arrays. First we look at building arrays from lists, and then at joining arrays together in various ways to make larger arrays. ## 5.1 Building Arrays by Shaping Lists The dyadic verb `$` (dollar) is called "Shape". The expression `(x $ y)` produces an array of the items of the list `y`, with shape `x`, that is, with dimensions given by the list `x`. For example:
`2 2 $ 0 1 2 3` | `2 3 $ 'ABCDEF'`
    0 1
    2 3
    ABC
    DEF
If the list `y` contains fewer than the number of items needed, then `y` is re-used in cyclical fashion to make up the number of items needed. This means that an array can be built to show some simple patterning, such as all elements being the same, for example.
`2 3 $ 'ABCD'` | `2 2 $ 1` | `3 3 $ 1 0 0 0`
    ABC
    DAB
    1 1
    1 1
    1 0 0
    0 1 0
    0 0 1
Instead of re-using values from `y`, a value to fill the array can be specified with the "Customize" conjunction `(!.)` (exclamation-mark dot).
       2 3 ($ !. '*') 'ab'
    ab*
    ***
The "Shape" verb, dyadic `$`, has a companion verb, "ShapeOf" (monadic `$`), which yields the list-of-dimensions, that is, shape, of its argument. To illustrate:
`A =: 2 3 $ 'ABCDEF'` | `$ A` | `a =: 'pqr'` | `$ a`
    ABC
    DEF
    2 3
    pqr
    3
An array can be of length zero in any of its dimensions. If `EL` is a zero-length, or empty, list then it has no items, and so, after appending an item to it, the result will have one item.
`EL =: 0$0` | `# EL` | `EL , 1`
    0
    1
Similarly, if `ET` is an empty table with no rows, and say, 3 columns, then after adding a row, the result will have one row.
`ET =: 0 3 $ 'x'` | `$ ET` | `$ ET , 'pqr'`
    0 3
    1 3
It sometimes occurs that we need to build a scalar. A scalar has no dimensions, that is, its dimension-list is empty. We can give an empty list as the left argument of `$` to make a scalar:
       (0$0) $ 99
    99
We said that `(x $ y)` produces an `x`-shaped array of the items of `y`. If `y` is a table, then each item of `y` will itself be a list (a row). Hence in general the shape of `(x$y)` will be not just `x`, but rather `x` followed by the shape of an item of `y`.
`A` | `3 $ A` | `$ 3 $ A`
    ABC
    DEF
    ABC
    DEF
    ABC
    3 3
The next sections look at building new arrays by joining together arrays we already have. ## 5.2 Appending, or Joining End-to-End Recall that any array can be regarded as a list of items, so that for example the items of a table are its rows. The verb , (comma) is called "Append". The expression `(x,y)` is a list of the items of `x` followed by the items of `y`.
       B =: 2 3 $ 'UVWXYZ'
       b =:   3 $ 'uvw'
`a` | `b` | `a , b` | `A` | `B` | `A , B`
    pqr
    uvw
    pqruvw
    ABC
    DEF
    UVW
    XYZ
    ABC
    DEF
    UVW
    XYZ
In the example of `(A,B)` above. the items of `A` are lists of length 3, and so are the items of `B`. Hence items of `A` are compatible with, that is, have the same rank and length as items of `B`. What if they do not? In this case the "Append" verb will helpfully try to stretch one argument to fit the other, by bringing them to the same rank, padding to length, and replicating scalars as necessary. This is shown the following examples. ### 5.2.1 Bringing To Same Rank Suppose we want to append a row to a table. For example, consider appending the 3-character list `b` (above) to the 2 by 3 table `A` (above) to form a new row.
`A` | `b` | `A , b`
    ABC
    DEF
    uvw
    ABC
    DEF
    uvw
Notice that we want the two items of `A` to be followed by the single item of `b`, but `b` is not a 1-item affair. We could do it by reshaping `b` into a 1 by 3 table, that is, by raising the rank of `b`. However, this is not necessary, because, as we see, the "Append" verb has automatically stretched the low-rank argument into a 1-item array, by supplying leading dimension(s) of 1 as necessary.
`A` | `b` | `A , (1 3 $ b)` | `A , b` | `b , A`
    ABC
    DEF
    uvw
    ABC
    DEF
    uvw
    ABC
    DEF
    uvw
    uvw
    ABC
    DEF
### 5.2.2 Padding To Length When the items of one argument are shorter than the items of the other, they will be padded out to length. Characters arrays are padded with the blank character, numerical arrays with zero.
`A` | `A , 'XY'` | `(2 3 $ 1) , 9 9`
    ABC
    DEF
    ABC
    DEF
    XY
    1 1 1
    1 1 1
    9 9 0
### 5.2.3 Replicating Scalars A scalar argument of "Append" is replicated as necessary to match the other argument. In the following example, notice how the scalar `'*'` is replicated, but the vector `(1 $ '*')` is padded.
`A` | `A , '*'` | `A , 1 $ '*'`
    ABC
    DEF
    ABC
    DEF
    ***
    ABC
    DEF
    *
## 5.3 Stitching, or Joining Side-to-Side The verb `,.` (comma dot) is called "Stitch". In the expression `(x ,. y)` each item of `x` has the corresponding item of `y` appended.
`a` | `b` | `a ,. b` | `A` | `B` | `A ,. B`
    pqr
    uvw
    pu
    qv
    rw
    ABC
    DEF
    UVW
    XYZ
    ABCUVW
    DEFXYZ
## 5.4 Laminating, or Joining Face-to-Face The verb `,:` (comma colon) is called "Laminate". In the expression `(x ,: y)`, if `x` and `y` are, say, two similar tables, then we can imagine the result as one table laid on top of the other to form a 3-dimensional array. Thus the arguments are joined along a new dimension, always of length 2. The result has two items, of which the first is `x` and the second is `y`. This means that the new dimension is the first.
`a` | `b` | `a ,: b` | `A` | `B` | `A ,: B`
    pqr
    uvw
    pqr
    uvw
    ABC
    DEF
    UVW
    XYZ
    ABC
    DEF

    UVW
    XYZ
## 5.5 Linking The verb ; (semicolon) is called "Link". It is convenient for building lists of boxes.
`'good' ; 'morning'` | `5 ; 12 ; 1995`
    +----+-------+
    |good|morning|
    +----+-------+
    +-+--+----+
    |5|12|1995|
    +-+--+----+
Notice how the example of `5;12;1995` shows that `(x;y)` is not invariably just `(< x),(< y)` . Since "Link" is intended for building lists of boxes, it recognises when its right argument is already a list of boxes. If we define a verb which does produce `(< x),(< y)`
       foo =: 4 : '(< x.) , (< y.)'
we can compare these two:
`1 ; 2 ; 3` | `1 foo 2 foo 3`
    +-+-+-+
    |1|2|3|
    +-+-+-+
    +-+-----+
    |1|+-+-+|
    | ||2|3||
    | |+-+-+|
    +-+-----+
## 5.6 Unbuilding Arrays We have looked at four dyadic verbs: "Append" `(,)`, "Stitch" `(,.)`, "Laminate" `(,:)` and "Link" `(;)`. Each of these has a monadic case, which we now look at. ### 5.6.1 Razing Monadic `;` is called "Raze". It unboxes elements of the argument and assembles them into a list.
`B =: 2 2 $ 1;2;3;4` | `; B` | `$ ; B`
    +-+-+
    |1|2|
    +-+-+
    |3|4|
    +-+-+
    1 2 3 4
    4
### 5.6.2 Ravelling Monadic `,` is called "Ravel". It assembles elements of the argument into a list.
`B` | `, B` | `$ , B`
    +-+-+
    |1|2|
    +-+-+
    |3|4|
    +-+-+
    +-+-+-+-+
    |1|2|3|4|
    +-+-+-+-+
    4
### 5.6.3 Ravelling Items Monadic `,.` is called "Ravel Items". It separately ravels each item of the argument to form a table.
`k =: 2 2 3 $ i. 12` | `,. k`
    0  1  2
    3  4  5

    6  7  8
    9 10 11
    0 1 2 3  4  5
    6 7 8 9 10 11
"Ravel Items" is useful for making a 1-column table out of a list.
`b` | `,. b`
    uvw
    u
    v
    w
### 5.6.4 Itemizing Monadic `,:` makes a 1-item array out of any array, by adding a leading dimension of `1`.
`A` | `,: A` | `$ ,: A`
    ABC
    DEF
    ABC
    DEF
    1 2 3
## 5.7 Arrays Large and Small For small arrays, where the contents can be listed on a single line, there are alternatives to using `$`, which avoid the need to give the dimensions explicitly.
`> 1 2 ; 3 4 ; 5 6` | `1 2 , 3 4 ,: 5 6`
    1 2
    3 4
    5 6
    1 2
    3 4
    5 6
To build large tables, a convenient method is as follows. First, here is a "utility" verb (that is, a verb which is useful for present purposes, but we don't need to study its definition now.)
       ArrayMaker =: ". ;. _2
The purpose of `ArrayMaker` is to build a numeric table row by row from the lines of a script.
       table =: ArrayMaker 0 : 0
    1 2 3
    4 5 6
    7 8 9
    )
`table` | `$ table`
    1 2 3
    4 5 6
    7 8 9
    3 3
(See Chapter 17 for an explanation of how `ArrayMaker` works). Arrays of boxes can also be entered from a script in the same way:
       X =:  ArrayMaker  0 : 0
    'hello' ; 1 2 3 ; 8
    'Waldo' ; 4 5 6 ; 9
    )
`X` | `$ X`
    +-----+-----+-+
    |hello|1 2 3|8|
    +-----+-----+-+
    |Waldo|4 5 6|9|
    +-----+-----+-+
    2 3
We have reached the end of Chapter 5.

NEXT
Table of Contents

Copyright © Roger Stokes 2000. This material may be freely reproduced, provided that this copyright notice and provision is also reproduced.

last updated 17Mar00