Compact if check involving multiple strings in Fortran [duplicate]

给你一囗甜甜゛ 提交于 2020-08-26 09:25:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!