Subsections


Slicing

In the previous section, it was mentioned that a subscript is associated with every element in a multiple. The lower-bound of the multiple for a dimension determines the minimum subscript for that dimension and the upper-bound for that dimension determines the maximum subscript. Thus there is a set of subscripts for each dimension. The individual elements can be accessed by quoting all the subscripts for that element. For example, the elements of the multiple

   []INT odds = (1,3,5)

can be accessed as odds[1], odds[2] and odds[3]. The first of these is read “odds sub one bus” (“bus” is the opposite of “sub”). In a multi-dimensional multiple, two or more subscripts are required to access a single element, the subscripts being separated by commas. For example, in the multiple

   [,]REAL rs = ((1.0, 2.0, 3.0),
                 (4.0, 5.0, 6.0))

rs[1,2] yields 2.0. Similarly, rs[2,3] yields 6.0. Thus one can declare

   REAL rs12 = rs[1,2],
        rs23 = rs[2,3]

Although, technically, a multiple with all its subscripts specified is called a slice, the term is usually reserved for a multiple with less than the maximum number of subscripts (in other words, at least one of the dimensions does not have a subscript). For example, using rs declared above, we can write

   []REAL srs = rs[1,]

which yields the multiple denoted by (1.0,2.0,3.0). The comma must be present in the slice on the right-hand side otherwise the compiler will report an error of “wrong number of indices”.

Vertical slicing is also possible. The phrase rs[,2] yields the multiple (2.0,5.0). In the context of the declaration

   [,]CHAR rs2 = (("a","b","c","d"),
                  ("e","f","g","h"),
                  ("i","j","k","l"))

the slice rs2[,3] yields the value "cgk" with a mode of []CHAR. Note, however, that vertical slicing is only possible for multiples with at least two dimensions. The multiple days, declared in the previous section, is one-dimensional and so cannot be sliced vertically.

In a 3-dimensional multiple, both 2-dimensional and 1-dimensional slices can be produced. Here are some examples:

   [,,]INT r3 = (((1,2),(3,4),((5,6),(7,8)));
   [,]INT r31 = r3[1,,],
          r32 = r3[,2,],
          r33 = r3[,,3];
   []INT r312 = r31[2,], r4 = r31[,2]

Exercises

3.5
The declaration
   [,]INT r = (( 1, 2, 3, 4),
               ( 5, 6, 7, 8),
               ( 9,10,11,12),
               (13,14,15,16))
is in force for this and the following exercise. Give the value of the following slices: Ans[*]
(a)
r[2,2]

(b)
r[3,]

(c)
r[,2 UPB r]

3.6
Write slices for the following values Ans[*]
(a)
10

(b)
(5,6,7,8)

(c)
(3,7,11,15)


Sian Mountbatten 2012-01-19