0

I need to use an IF condition to determine whether an integer in an array (A) matches with an integer in another array (B) before proceeding with the code. The arrays are of unequal length and contain unique values which are all positive.

I know I can find matching values using:

DO I = 1,SIZE(A)
Match(J)=(ANY(A(I)==B))

However this format isn't accepted by the IF...THEN construct. I've not yet found a way to implement it looking online and general testing. What am I missing here?

EDIT: As as an alternative I tried the below code:

   INTEGER :: I , K, B(900),C(900),I1,ID3, IP
   INTEGER, INTENT(IN)           :: A, N

       OPEN(12,FILE='../B.dat')
       DO I=1,817
          READ(12,*)B(I),C(I)
       END DO

       DO I = 1,SIZE(A)
        DO K = 1,SIZE(B)
         IF (I.EQ.K) THEN
             DO IP = N-9,N
                ID3 = I1 +A*(IP-1)
         END DO
         END DO
         END DO

However K changes throughout the loop, and so the code doesn't work for matching. I then tried:

DO I = 1, SIZE(A)
  I1 =ID(I)
    DO IP = N-9,N
      ID3 = I1 +A*(IP-1)
    END DO

However I received a segmentation error:

_____________ runcode::main: : |runCode: Fail to run |C:\TELEMAC\VEG\May\PRIVEtest.cas_2018-05-02-16h57min04s\out_testMedit.exe |~~~~~~~~~~~~~~~~~~ |Program received signal SIGSEGV: Segmentation fault - invalid memory referen ce. | |Backtrace for this error: |#0 ffffffffffffffff |~~~~~~~~~~~~~~~~~~

2
  • 1
    However this format isn't accepted by the IF...THEN test Show us the IF you tried then. Show us the error messages. Show the code around, at least close your loops and define your variables (see minimal reproducible example). It is often much easier and more useful to fix existing code than writing a full code from scratch for you (and often that is too much to ask from us anyway). Commented May 2, 2018 at 14:56
  • We need a complete example not just some code snippets. Furthermore which command line options (and compiler) did you use. Did you use the option of the compiler for "check boundaries"? Commented May 2, 2018 at 17:55

1 Answer 1

4

You have far too many irrelevant variables in your example, while at the same time not giving us the information to properly assess what you want.

If you simply want to check whether two arrays A and B of possibly unequal size share at least one element, then you can use this construct:

any([(any(A(i) == B), i = 1, size(A))])

Let's go into the details:

any(A(i) == B)

This checks whether the ith element of A is anywhere in B. Returns a logical. I then create a temporary array of this for all elements of A:

[(any(A(i) == B), i = 1, size(A))]

Then just put an any() around that, and you can absolutely use that in an IF statement:

program my_any

    implicit none
    integer :: A(3), B(4), i

    A = [1, 2, 3]
    B = [6, 3, 4, 10]

    if (any([(any(A(i) == B), i=1, size(A))])) then
        print *, "YES"
    else
        print *, "NO"
    end if

end program my_any
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.