# Problems with FINDLOC command

**URL:** <https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644>\
**Category:** Help\
**Created:** [January 24, 2022, 1:46pm UTC](https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644 "2022-01-24T13:46:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CConde](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CConde](https://fortran-lang.discourse.group/u/CConde)\
**Post date:** [January 24, 2022, 1:46pm UTC](https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644/1 "2022-01-24T13:46:22Z")

</div>

Dear friends,

Please, I would like to request your help with this problem: given a one dimensional array I have to find the first value that is larger than 0. I know that the way to go is to use FINDLOC but I don’t know how to implement it since, as far as I know, its syntax is FINDLOC( array, scalar), so I cannot figure it out how to include the condition \> 0.

Is it possible to obtain a result as an integer or it will always be an integer array?

Many thanks for your help.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [January 24, 2022, 1:55pm UTC](https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644/2 "2022-01-24T13:55:35Z")

</div>

Hello @CConde,

to obtain a scalar integer as return value, you need to supply the `dim` argument of [`findloc`](https://gcc.gnu.org/onlinedocs/gfortran/FINDLOC.html):

> If `DIM` is present and `ARRAY` has a rank of one, the result is a scalar.

As the `array` argument you supply an array of logical values obtained from testing your condition. Here is a minimal working example.

```auto
implicit none
integer :: a(5), pos
a = [-2, -1, 0, 1, 2]
pos = findloc(a > 0, .true., dim = 1)
print *, pos ! 4
end

```

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 24, 2022, 2:19pm UTC](https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644/3 "2022-01-24T14:19:40Z")

</div>

This task occurs often enough that I have defined a function `first_true`

```auto
pure function first_true(tf) result(i1)
! return the location of the first true element in tf(:), 0 if none true
logical, intent(in) :: tf(:)
integer :: i1
integer :: i
i1 = 0
do i=1,size(tf)
   if (tf(i)) then
      i1 = i
      return
   end if
end do
end function first_true

```

and can write

`pos = first_true(a>0)`

Maybe this utility function is suitable for stdlib.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [January 24, 2022, 2:25pm UTC](https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644/4 "2022-01-24T14:25:15Z")

</div>

Now that you mention it, I see some clash with the chosen name for the `trueloc` function that was added already ([true\_pos · Issue #568 · fortran-lang/stdlib · GitHub](https://github.com/fortran-lang/stdlib/issues/568)). (If I’m not mistaken, the function in stdlib returns all index positions belonging to true elements.)

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [January 24, 2022, 2:51pm UTC](https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644/5 "2022-01-24T14:51:45Z")

</div>

Maybe it can be called `firstloc` to be consistent with `trueloc` and `falseloc`.

---

<div class="post-metadata">

**Author:** ![CConde](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CConde](https://fortran-lang.discourse.group/u/CConde)\
**Post date:** [January 24, 2022, 3:29pm UTC](https://fortran-lang.discourse.group/t/problems-with-findloc-command/2644/6 "2022-01-24T15:29:13Z")

</div>

@ivanpribec, @Beliavsky, many thanks for your help.
