简体   繁体   中英

Fortran "ANY" function error while checking if a array contains a value

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM