015. merge(x, y, cond)
#
topic: Conditionals
merge(x, y, cond)
returns x
if the condition cond
is true, otherwise y
.
We can get the same result as the previous tip:
integer :: i
do i = -1, 1
print *, i
! Note: "zero" padded to have same length as "negative"
print *, merge("negative", merge("zero ", &
"positive", i == 0 ), i < 0)
end do
end
-1
negative
0
zero
1
positive
Similar to the three-argument usage of
numpy.where
.
merge(x,y,condition) returns x if condition is true, otherwise y. Same output as previous:
— FortranTip (@fortrantip) December 18, 2021
integer :: i
do i=-1,1
print*,i
! "zero" padded to have same length as "negative"
print*,merge("negative",merge("zero ", &
"positive",i==0),i<0)
end do
end
- 1
Compiled using
GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
with no flags