I have the following code that counts the number of consecuitive logicals from the first one. I want to simplify this function. Have been thinking that perhaps I can use recursion somehow but now quite sure. Is that possilble?
Function count_present &
( &
p1, p2, p3, p4, p5, p6, p7, p8 &
) &
Result (n)
Logical, Intent (in) :: p1
Logical, Intent (in), Optional :: p2, p3, p4, p5, p6, p7, p8
Integer :: n
n = 0
If (Present (p8)) Then
If (p8) Then; n = 8
Else If (p7) Then; n = 7
Else If (p6) Then; n = 6
Else If (p5) Then; n = 5
Else If (p4) Then; n = 4
Else If (p3) Then; n = 3
Else If (p2) Then; n = 2
Else If (p1) Then; n = 1
End If
Else If (Present (p7)) Then
If (p7) Then; n = 7
Else If (p6) Then; n = 6
Else If (p5) Then; n = 5
Else If (p4) Then; n = 4
Else If (p3) Then; n = 3
Else If (p2) Then; n = 2
Else If (p1) Then; n = 1
End If
Else If (Present (p6)) Then
If (p6) Then; n = 6
Else If (p5) Then; n = 5
Else If (p4) Then; n = 4
Else If (p3) Then; n = 3
Else If (p2) Then; n = 2
Else If (p1) Then; n = 1
End If
Else If (Present (p5)) Then
If (p5) Then; n = 5
Else If (p4) Then; n = 4
Else If (p3) Then; n = 3
Else If (p2) Then; n = 2
Else If (p1) Then; n = 1
End If
Else If (Present (p4)) Then
If (p4) Then; n = 4
Else If (p3) Then; n = 3
Else If (p2) Then; n = 2
Else If (p1) Then; n = 1
End If
Else If (Present (p3)) Then
If (p3) Then; n = 3
Else If (p2) Then; n = 2
Else If (p1) Then; n = 1
End If
Else If (Present (p2)) Then
If (p2) Then; n = 2
Else If (p1) Then; n = 1
End If
Else
If (p1) n = 1
End If
End Function count_present
See Question&Answers more detail:os