How to check if Fortran array contains value?

Pureferret picture Pureferret · Dec 1, 2011 · Viewed 24.6k times · Source

I've seen this asked for other languages, but having just found out how nicely Fortran can handle arrays, I thought there might be an easy way to do this without loops.

Currently I'm searching over a 3D array looking at 'nearest neighbours' to see if they contain the letter 'n', and whenever it finds this value, I want it to perform some clusterLabel assignment (which isn't relevant for this question)

I wanted to use if(lastNeighArray.eq."n") then...<rest of code> but for obvious reasons it doesn't like checking an array against a value. Neither does it like me using lastNeighArray(:), even though I'd like it to check each of the elements one at a time. where(lastNeighArray.eq."n") doesn't work as I have a case statement inside the where loop and I get the error WHERE statements and constructs must not be nested.

So I'm a little stuck. What I really want is something like when(lastNeighArray.eq."n") but that doesn't exist.

I've also looked at any and forall but they don't seem like the right choice.

Answer

Vladimir F picture Vladimir F · Dec 1, 2011

ANY should actually be the right choice

if ( ANY( lastNeighArray=="n" ) ) then

there is also ALL if you wanted the whole array to contain that value.