type, DIMENSION(bound)[, attr] :: name
type [, attr] :: name(bound)
Where:
TYPE
- intrinsic or derived type of the elements,
DIMENSION
- (attribute used to specify the dimensions of the array (optional),
bound
- lower and upper bounds for each dimension,
attr
- other type attributes (as required),
name
- name of the array variable.
INTEGER, DIMENSION(6) :: a
REAL, DIMENSION(0:9) :: b
LOGICAL, DIMENSION(2,2) :: yes_no
INTEGER :: a(6)
REAL :: b(0:9)
LOGICAL :: yes_no(2,2)
Further examples:
INTEGER, DIMENSION(8) :: x, y
REAL :: alpha(1:3), beta(4:9)
REAL, DIMENSION(0:5,12:45,6) :: data
CHARACTER(len=10) :: names(25)
TYPE(point)
REAL :: position(3)
END TYPE(point)
TYPE(point) :: object(10)
Each array subscript is either:
array( index [...] )
array( [lower]:[upper][:step] [...])
lower
and upper
default to the declared dimensions and step
defaults to 1.
REAL, DIMENSION(8) :: a
INTEGER, DIMENSION(5,4) :: b
REAL, DIMENSION(8) :: a
INTEGER, DIMENSION(5,4) :: b
(/ list /)
REAL, DIMENSION(9) :: a
INTEGER, DIMENSION(3) :: random
...
random = (/6,3,8/)
a( random ) = 0.0
a( (/7,8,9/) ) =
1.2
REAL, DIMENSION(5) :: a
INTEGER, DIMENSION(3) :: list
...
list = (/2,3,2/)
a(list) = (/ 1.1,1.2,1.3 /) !illegal
REAL, DIMENSION(3,5) :: a

REAL, DIMENSION(100) :: a, b, c
REAL :: d(10,10)=0.0
...
b = 2*a+4 !array expression
a = 2.0 !scalar broadcast
c = b*a !element*element
...
c = d !illegal
REAL, DIMENSION(10) :: alpha, beta
REAL :: gamma(20)
...
alpha(1:5) = 2.0
alpha(1:10:2) = beta(1:5)/6
alpha(2:10) = alpha(1:9)
...
gamma(11:20) = beta
REAL :: num
REAL, DIMENSION(3,3) :: a
INTEGER :: length(5)
CHARACTER(len=10) :: c(5)
...
x = SQRT( num ) !single variable
a = SQRT( a ) !all elements
...
!find string length for each element
!no trailing blanks
length = LEN( TRIM( c ) )
INTEGER :: a(5)=(/1,2,1,1,3/)
...
a( 1:COUNT(a==1) ) = 0
!new a(0,0,0,1,3)
...
a( 1:COUNT(a==4) ) = 0
!unchanged a(0,0,0,1,3)
array = (/ list /)
Where list
may be:
INTEGER :: a(6)=(/1,2,3,6,7,8/)
REAL :: b(2)=(/SIN(x),COS(x)/)
INTEGER :: c(5)=(/0,a(1:3),4/)
REAL :: d(100)=(/REAL(i),i=1,100/)
RESHAPE( list, shape [,PAD] [,ORDER])
Where:
list
is a rank 1 array or constructor containing the data,
INTEGER, DIMENSION(2,3) :: a
a = RESHAPE( (/i,i=0,5/), (/3,2/) )
DATA variable / list /...
INTEGER :: a(4), b(2,2), c(10)
...
DATA a/4,3,2,1/ !by value
...
DATA a/4*0/ !whole array
...
DATA b(1,:)/0,0/ !by section
DATA b(2,:)/1,1/
...
DATA (c(i),i=1,10,2)/5*1/
DATA (c(i),i=2,10,2)/5*2/
DATA
statement may be used for other variables as well as arrays.
WHERE
statement allows a single array assignment only if a logical condition is true.
WHERE( condition ) statement
INTEGER :: a(2,3,4)
...
WHERE( a<0 ) a=0
WHERE( 3*a>10 ) a=99
WHERE
construct allows array assignment(s) only if a logical condition is true, and alternative array assignment(s) if false.
WHERE( condition )
block1
[ELSEWHERE
block2]
ENDWHERE
INTEGER :: b(8,8)
WHERE( b<=0 )
b = 0
ELSEWHERE
b = 1/b
ENDWHERE
ALL( condition, [DIM] )
LOGICAL :: test, test2(2), test3(3)
REAL, DIMENSION(3,2) :: a
DATA a/5,9,6,10,8,12/
...
test = ALL( a>5 ) !false
test2 = ALL( a>5, DIM=1 ) !f,t,t
test3 = ALL( a>5, DIM=2 ) !f,t
SIZE( array, [DIM] )
REAL, DIMENSION(3,2) :: a
...
num = SIZE( a ) !num=6
num = SIZE( a, DIM=1 ) !num=2
num = SIZE( a, DIM=2 ) !num=3
SPREAD( array, DIM, NCOPIES )
REAL, DIMENSION(3) :: a=(/2,3,4/)
REAL, DIMENSION(3,3) :: b, c
...
b = SPREAD( a, DIM=1, NCOPIES=3 )
c = SPREAD( a, DIM=2, NCOPIES=3 )
MAXLOC( array [, MASK] )
REAL :: a(5)
a = (/2.0,8.0,5.0,3.0,4.0/)
...
num = MAXLOC( a ) !num=2
num = MAXLOC( a, MASK=a<5 ) !num=5
num = MAXLOC( a(2:4) ) !num=1
ALL
- True if all elements are true.
ANY
- True if any element is true.
COUNT
- Number of true elements.
MAXVAL
- Maximum element value.
MINVAL
- Minimum element value.
PRODUCT
- Product of array elements.
SUM
- Sum of array elements.
ALLOCATED
- True if array has storage allocated.
LBOUND
- Lower bounds of array.
SHAPE
- Shape of array (or scalar).
SIZE
- Size of array.
UBOUND
- Upper bounds of array.
MERGE
- Merge arrays.
PACK
- Pack elements into a vector.
SPREAD
- Duplicate an array section.
UNPACK
- Unpack elements of a vector.
RESHAPE
- Reshapes array.
MAXLOC
- Location of maximum element.
MINLOC
- Location of minimum element.
CSHIFT
- Circular shift.
EOSHIFT
- End-off shift.
TRANSPOSE
- Transpose matrix.
DOT_PRODUCT
- Calculate dot product.
MATMUL
- Matrix multiplication.