0

Is there a way to check if a string is equal to any of the list together instead of individually explicitly check with a ==? For example,

if(color=='violet' .or. color=='indigo' .or. color=='blue' .or.&
   color=='green' .or. color=='yellow' .or. color=='orange' .or. color=='red') then
   print *, "It is a rainbow color"
end if

Is there a way compact way to do this? Something like if(color=='violet|indigo|blue|green|yellow|orange|red') ?

SKPS
  • 5,433
  • 5
  • 29
  • 63
  • 1
    This question is essentially the same as [this other one](https://stackoverflow.com/q/47828183/3157076) (but can't be marked a duplicate of it). The essential premise is that you use an array, (constructing a character array [like this](https://stackoverflow.com/q/21552430/3157076)), and use [`any`](https://stackoverflow.com/q/8340592/3157076). – francescalus Aug 13 '20 at 09:14

1 Answers1

2

You can put the colors into an array and use any.

if (any(color == [character(6) :: "violet","indigo","blue","green","yellow","orange","red"]))
SKPS
  • 5,433
  • 5
  • 29
  • 63
  • I am sure my question has been answered already but the question you linked is different – SKPS Aug 13 '20 at 08:47
  • @SKPS I did not actually link anything. This one is the same https://stackoverflow.com/q/47828183/3157076 but because it hasn't got an answer, it cannot be a duplicate target. Perhaps we can make it a target in the other direction now, even if the other one is much older. – Vladimir F Героям слава Aug 13 '20 at 11:51