In my code:
DO i=1,numJog,1
IF(val(i) .EQV. .TRUE.)THEN
DO j=1,contVenc,1
result(j) = i
END DO
END IF
END DO
Where val
is a logical array, and result
is a integer array.
For example, if val
is:
F
T
F
T
Then, i=2
and i=4
.
But the result
array just write 4 twice. For example:
DO i=1,contVenc,1
WRITE(*,*) result(i)
END DO
The result is:
4
4
Instead of
2
4
If I make some changes in my code like:
DO i=1,numJog,1
IF(val(i) .EQV. .TRUE.)THEN
WRITE(*,*) i
END IF
END DO
The result is:
2
4
As I wanted.
Conclusion, I think this second loop is causing this problem.
Yes, your second loop is at fault here. You haven't said what
contVenc
is, but it crucially doesn't change at any point in the fragment you have there. That just means that the same elements ofresult
are being assigned to whenever you have a.TRUE.
inval
.In your case they are both set to
2
for the first.TRUE.
and are then both set to4
for the second.You are more likely to mean something like (with extra tidying):
But then, I'd just use
PACK
. Your intended loop has the same effect asAgain hoping that
result
is large enough.That said, if
numJog
is just the size of the arrayval
(that is, you aren't just doing this on a sub-array) then, as High Performance Mark comments,avoids tracking this size separately.
Finally, with
result
an allocatable (Fortran 2003) array you needn't even (but still can) worry about counting the number of wanted indices and that the array is sufficiently large: