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?
This task occurs often enough that I have defined a function first_true
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.
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). (If I’m not mistaken, the function in stdlib returns all index positions belonging to true elements.)