简体   繁体   English

Fortran "ANY" function 检查数组是否包含值时出错

[英]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.您的问题是 ANY 是缩减操作,它需要存储在逻辑数组中的许多值并将它们缩减为单个值,在本例中为 value.True。 is any of the values in the array are true, or.False.是数组中的任何值是 true 还是 .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.您的直接问题是您只是为 ANY 提供标量值,而不是数组,因此会出现错误。 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.但是这里可能有一种方法可以使用 ANY,这可能是解决您正在做的事情的最佳方法。 However without the rest of the code including the variable declarations it is impossible to say.但是,如果没有包括变量声明在内的代码 rest,就不可能说了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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