如果在Fortran中检查涉及多个字符串,则压缩
问题描述:
是否有办法一起检查一个字符串是否等于列表中的任何一个,而不是分别用==
进行显式检查?例如,
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
有没有一种紧凑的方法可以做到这一点?类似于if(color=='violet|indigo|blue|green|yellow|orange|red')
吗?
Is there a way compact way to do this? Something like if(color=='violet|indigo|blue|green|yellow|orange|red')
?
答
您可以将颜色放入数组中并使用any
.
You can put the colors into an array and use any
.
if (any(color == [character(6) :: "violet","indigo","blue","green","yellow","orange","red"]))