mecej4,
Did you compile your example before posting ?
looks like you didn't, so not bad for no checking.
Here is my (tested) example showing mecej4's new F90 way and mine and (Eddie's?) old way using DO loops.
program FindValuesGreaterThan2
integer, parameter :: n = 10
real :: testArray(n)
testArray = [1.0, 3.0, 2.0, 5.0, 0.0, 4.0, 2.5, 6.0, 1.5, 3.5]
call old_DO_way ( testArray,n )
call new_pack_way ( testArray,n )
end program FindValuesGreaterThan2
subroutine new_pack_way ( testArray,n )
implicit none
integer :: n
real :: testArray(n)
integer :: indices(n)
integer :: nsel, i
integer, allocatable :: selIdx(:)
! Initialize the test array index
indices = [(i,i=1,n)]
! Select and list elements > 4.7
selIdx = pack(indices,testArray > 4.7)
nsel = size(selIdx)
print '(1x,2i3,F4.1)',(i,selIdx(i),testArray(selIdx(i)),i=1,nsel)
end subroutine new_pack_way
SUBROUTINE old_DO_way ( testArray,n )
implicit none
integer :: n
real :: testArray(n)
integer :: nsel, i
integer :: selIdx(n)
! generate a list of values
nsel = 0
do i = 1,n
if ( testArray(i) <= 4.7 ) cycle
nsel = nsel+1
selIdx(nsel) = i
end do
print '(1x,2i3,F4.1)',(i,selIdx(i),testArray(selIdx(i)),i=1,nsel)
end SUBROUTINE old_DO_way
I still prefer to see DO loops to show what is happening.
After these comments, I hope my code doesn't need correcting !