Lahey/Fujitsu Fortran

MERGE Function

Description

The MERGE function chooses alternative values based on the value of a mask.

Syntax

MERGE (tsource, fsource, mask)

Arguments

tsource is an INTENT(IN) scalar or array and can be of any type.

fsource is an INTENT(IN) scalar or array of the same type and type parameters as tsource.

mask is an INTENT(IN) scalar or array of type LOGICAL.

If more than one argument is an array, all arrays must have the same shape.

Result

The result is of the same type and type parameters as tsource.

If all arguments are scalar, the value is tsource if mask is true, and fsource otherwise.

If any argument is an array, the result is an array with the same shape. Each element of the result is as if the scalar MERGE function was called for each corresponding element of the array arguments.

Example

integer :: i=1, j= 2 integer :: m(2,2)=reshape((/1,2,3,4/),shape(m)) integer :: n(2,2)=reshape((/4,3,2,1/),shape(n)) write(*,10) m ! writes 1 2 ! 3 4 write(*,10) n ! writes 4 3 ! 2 1 write(*,10) merge(m,n,m < n) ! writes 1 2 ! 2 1 write(*,'(2l3)') merge(.true.,.false.,m < n) ! writes T T ! F F 10 format(2i3)