Often, when working with Fortran sources with WHERE constructs, I have felt handicapped by not being able to evaluate one expression for the subset that satisfy the condition clause and a different expression for the subset that do not, with both expressions having access to the indices of the elements that satisfy or do not satisfy the condition. Here is an example:
program whereIdx
implicit none
integer, parameter :: N = 5
integer i, idx(N),ival(N)
idx = [ (i, i=1,N) ]
ival = [-3, -1, 1, 3, 5]
where (ival > 2)
ival = ival+idx*2
elsewhere
ival = ival-idx*3
end where
print '(5I3)',ival
end program
It would be possible to make the code shorter and more readable if we are able to write, instead, something similar to
program whereIdx
implicit none
integer, parameter :: N = 5
integer i, ival(N)
integer, allocatable :: idx(:)
ival = [-3, -1, 1, 3, 5]
where (ival > 2, INDEX = idx)
ival = ival+idx*2 ! expression uses indices of .TRUE. elements of ival > 2
elsewhere
ival = ival-idx*3 ! expression uses indices of .FALSE. elements of ival > 2
end where
print '(5I3)',ival
end program
Your opinions, please. Thanks.