next up previous contents
Next: Printing Arrays Up: Arrays Previous: Visualising Array Sections

 

Array Sections

The general form of a subscript-triplet specifier is::

 
[< bound1 >]:[< bound2 >][:< stride >]

The section starts at < bound1 > and ends at or before < bound2 >. < stride > is the increment by which the locations are selected. < bound1 >, < bound2 > and < stride > must all be scalar integer expressions. Thus

   A(:)        ! the whole array
   A(3:9)      ! A(m) to A(n) in steps of 1
   A(3:9:1)    ! as above
   A(m:n)      ! A(m) to A(n)  
   A(m:n:k)    ! A(m) to A(n) in steps of k
   A(8:3:-1)   ! A(8) to A(3) in steps of -1
   A(8:3)      ! A(8) to A(3) step 1 => Zero size
   A(m:)       ! from A(m) to default UPB
   A(:n)       ! from default LWB to A(n)
   A(::2)      ! from default LWB to UPB step 2
   A(m:m)      ! 1 element section
   A(m)        ! scalar element - not a section
are all valid.

If the upper bound (< bound2 >) is not a combination of the lower bound plus multiples of the stride then the actual upper bound is different to that stated; this is the same principle that is applied to DO-loops.

Another similarity with the DO-loops is that when the stride is not specified it is assumed to have a value of 1. In the above example, this means that A(3:8) is the same as A(3:8:1) but A(8:3) is a zero sized section and A(8:3:-1) is a section that runs backwards. Zero strides are not allowed.

Other bound specifiers can be absent too, if < bound1 > or < bound2 > is absent then the lower or upper bound of the dimension (as declared) is implied, if both are missing then the whole dimension is assumed.

Let us examine the above sections in detail,

Now try this question gif

Return to corresponding overview page gif


next up previous contents
Next: Printing Arrays Up: Arrays Previous: Visualising Array Sections

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.