【发布时间】:2021-02-02 10:51:32
【问题描述】:
我想使用 DATA 语句定义一些长度为两个字符的变量,直接在 DATA 语句中连接两个引用单个字符的命名常量。
有可能吗?如果是这样,正确的语法是什么?有没有更好、更简洁的方法来做到这一点?
例子
use m_ascii_chars ! this defines ch_* stuff as single character named constants
...
character(2) :: pairs(100)
...
DATA pair(1) / ch_plus // ch_verticalbar / ! would be the best one, but it does not work,
! given the meaning of the slash in the DATA
! statement
DATA pair(1) / ( ch_plus // ch_verticalbar ) / ! does not work !!
DATA pair(1) / [ ch_plus // ch_verticalbar ] / ! does not work !!
! This works, but it is rather verbose
DATA pair(1)(1:1) / ch_plus /
DATA pair(1)(2:2) / ch_verticalbar /
...
! Of course, this works too, but does not fit the requirements.
DATA pair(1) / '+|' /
【问题讨论】:
-
我强烈建议停止使用
DATA,它真的已经过时了。请展示一个更完整的示例并解释“不起作用”对您实际上意味着什么。有什么错误信息吗? -
我知道,实际上 DATA 语句不是我的习惯,我很乐意避免使用它(如我问题的最后一部分)。是的,编译器会根据我使用的(错误的)语法发出不同的错误消息(不值得报告)。我不想定义一个大的初始化数组,我只想使用命名常量来定义每个元素。假设: ` 整数,参数 :: PLUS_VERTICALBAR = 1 ... pair(PLUS_VERTICALBAR) = ch_plus // ch_verticalbar ... ` 我可能会在初始化过程中这样做,但我想在编译时这样做 *as使用 DATA 状态)
-
@francescalus 是的,在示例中,我在变量数组对上使用了 DATA。将对定义为参数(使用不同的语法)会更好。
标签: fortran initialization string-concatenation