问题
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')
?
回答1:
You can put the colors into an array and use any
.
if (any(color == [character(6) :: "violet","indigo","blue","green","yellow","orange","red"]))
来源:https://stackoverflow.com/questions/63390969/compact-if-check-involving-multiple-strings-in-fortran