SELECT RANK construct facilitates use of assumed rank objects within Fortran.
It has the syntax
[ construct-name ] SELECT RANK ( [ assoc_name => ] assumed-rank-variable-name )
[ rank-stmt
block ]...
END SELECT [ construct-name ]
where rank-stmt is one of:
RANK ( scalar-int-constant-expression ) [ construct-name ] RANK ( * ) [ construct-name ] RANK DEFAULT [ construct-name ]In any particular
SELECT RANK construct, there must not be more than one RANK DEFAULT statement,
or more than one RANK (*) statement, or more than RANK (integer) with the same value integer
expression.
If the assumed-rank variable has the ALLOCATABLE or POINTER attribute, the RANK (*) statement
is not permitted.
The block following a RANK statement with an integer constant expression is executed if
the assumed-rank variable is associated with a non-assumed-rank actual argument that has that rank,
and is not an assumed-size array.
Within the block it acts as if it were an assumed-shape array with that rank.
The block following a RANK (*) is executed if the ultimate argument is an assumed-size array.
Within the block it acts as if it were declared with bounds ‘(1:*)’; if different bounds
or rank are desired, this can be passed to another procedure using sequence association.
The block following a RANK DEFAULT statement is executed if no other block is selected.
Within its block, it is still an assumed-rank variable, i.e. there is no change.
Here is a simple example of the SELECT RANK construct.
Program select_rank_example
Integer :: a = 123, b(1,2) = Reshape( [ 10,20 ], [ 1,2 ] ), c(1,3,1) = 777, d(1,1,1,1,1)
Call show(a)
Call show(b)
Call show(c)
Call show(d)
Contains
Subroutine show(x)
Integer x(..)
Select Rank(x)
Rank (0)
Print 1,'scalar',x
Rank (1)
Print 1,'vector',x
Rank (2)
Print 1,'matrix',x
Rank (3)
Print 1,'3D array',x
Rank Default
Print *,'Rank',Rank(x),'not supported'
End Select
1 Format(1x,a,*(1x,i0,:))
End Subroutine
End Program
This will produce the output
scalar 123 matrix 10 20 3D array 777 777 777 Rank 5 not supported