0

I wish to check if one 2D array contains a value from other 1D array.

   do i=1,nlines
   do j=1,nchecks(i)
   if (type(i).eq.4) then
   do k=1,nlines
   do l=1,nchecks(k)
   if (type(k).eq.3) then
   if (ANY(con(i,j)==id(k))) then
   ...

But I face following error:

test1.f(98): error #6361: An array-valued argument is required in this context.   [ANY]
       if (ANY(conn(i,j)==id2(k))) then

What am I doing wrong? I also tried something like

   do i=1,nlines
   do j=1,nchecks(i)
   if (type(i).eq.4) then
   r1=conn(i,j)
   do k=1,nlines
   do l=1,nchecks(k)
   if (type(k).eq.3) then
   if (ANY(r1==id(k))) then
   ...

But this also brought the same error. All variables are properly defined, and no mistakes in format. Am I using ANY command in wrong way?

exsonic01
  • 615
  • 7
  • 27
  • 1
    Yes, you are using `ANY` incorrectly. At the moment you are just testing individual scalar elements of the arrays, so no `ANY` required. However, we don't know what's in the loops so it's impossible to know what you really want to do. Also, you can probably hoist those `if` tests on `i` and `k` outside the inner `j` and `l` loops respectively and/or reorder the loops by the looks. – RussF Apr 24 '20 at 03:22
  • 1
    Second @RussF, also please don't say "All variables are properly defined", show them - variable declarations are vital to understanding a problem. But by far best, as RussF implies, is to show a short, complete program which displays the problem you are having – Ian Bush Apr 24 '20 at 06:37
  • This - https://stackoverflow.com/questions/29631649/efficient-way-to-see-if-a-set-of-values-appear-in-an-array/29631745 - may be useful. It may even be a duplicate but I haven't checked closely. – High Performance Mark Apr 24 '20 at 07:20
  • No, it's not a duplicate, but it is closely related – Ian Bush Apr 24 '20 at 08:05
  • Amazed this has been closed. An answer, one which addresses the immediate problem, has been given in the comment by Russ, extra detail will only enable to us to give a better answer. Voted to reopen – Ian Bush Apr 24 '20 at 08:30
  • Last thing I say as even in these times the grumpies discourage chat, but my view is an answer can be given, but the best answer requires more info – Ian Bush Apr 24 '20 at 09:09
  • 1
    Well, looks like the smilers are winning right now @IanBush, the question's been reopened :-) – High Performance Mark Apr 24 '20 at 11:46
  • Damn. Feel honour bound to answer now. After my daily exercise ... – Ian Bush Apr 24 '20 at 12:35
  • @IanBush: for me answering would be my daily exercise ! – High Performance Mark Apr 24 '20 at 14:05

1 Answers1

2

Your problem is that ANY is a reduction operation, it takes many values stored in a logical array and reduces them down to a single value, in this case the value .True. is any of the values in the array are true, or .False. if all of them are false. Here's a very simple example

ian@eris:~/work/stack$ cat any.f90
Program Any_test

  Implicit None

  Write( *, * ) Any( [ .True. , .False. ] )
  Write( *, * ) Any( [ .False., .False. ] )

End Program Any_test

ian@eris:~/work/stack$ gfortran -std=f2008 -Wall -Wextra -fcheck=all any.f90 
ian@eris:~/work/stack$ ./a.out
 T
 F

Your immediate problem is that you are just providing ANY with a scalar value, not an array, hence the error. Simply

if (r1==id(k)) then

will fix the immediate problem.

But there is a probably way in here where you could use ANY, and this might be the best way to address what you are doing. However without the rest of the code including the variable declarations it is impossible to say.

Ian Bush
  • 6,996
  • 1
  • 21
  • 27