# Adding optional INDEX clause to WHERE construct

**URL:** <https://fortran-lang.discourse.group/t/adding-optional-index-clause-to-where-construct/5598>\
**Category:** Language enhancement\
**Created:** [April 18, 2023, 8:34pm UTC](https://fortran-lang.discourse.group/t/adding-optional-index-clause-to-where-construct/5598 "2023-04-18T20:34:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [April 18, 2023, 8:34pm UTC](https://fortran-lang.discourse.group/t/adding-optional-index-clause-to-where-construct/5598/1 "2023-04-18T20:34:19Z")

</div>

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:

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![nshaffer](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nshaffer/32/161_2.png) [@nshaffer](https://fortran-lang.discourse.group/u/nshaffer)\
**Post date:** [April 19, 2023, 2:28am UTC](https://fortran-lang.discourse.group/t/adding-optional-index-clause-to-where-construct/5598/2 "2023-04-19T02:28:52Z")

</div>

To my taste, both the original and proposed are no better than a loop.

```auto
  implicit none
  integer, parameter :: n = 5
  integer :: i, ival(n)
  integer, allocatable :: idx(:)
  ival = [-3, -1, 1, 3, 5]
  do i = 1, n
    if (ival(i) > 2) then
      ival(i) = ival(i) + 2*i
    else
      ival(i) = ival(i) - 3*i
    end if
  end do
  print '(5i3)', ival
end

```

I’m generally not a fan of the WHERE construct, though, so take my opinion with a grain of salt.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [April 19, 2023, 3:01am UTC](https://fortran-lang.discourse.group/t/adding-optional-index-clause-to-where-construct/5598/3 "2023-04-19T03:01:32Z")

</div>

I couldn’t figure out what your code was doing, neither the original, nor the proposed version, until I saw the loop in @nshaffer’s answer, which is clear to me. I personally use `where` when things are relatively simple, but once it becomes complicated like here, I prefer a loop. A good compiler should be able to make the performance of both equivalent.
