Fortran 90 中循环变量名
我有大量不同维度的数组。不过,我按顺序命名它们,例如 var1、var2 等等。为了阅读它们,我有兴趣以如下方式循环它们:
do i=1,n
read(1,*) var<i>
enddo
当然,由于数组具有不同的维度,我不能简单地添加一个维度并将它们折叠成一个维度。
我的印象是 Fortran 不允许将字符串转换为变量名或类似的东西,所以有人知道如何处理这个问题吗?
谢谢!
I have a large amount of arrays of different dimensions. However, I name them with a sequential order, say var1, var2, and so on. In order to read them, I would be interested in looping over them in something like:
do i=1,n
read(1,*) var<i>
enddo
Of course, since the arrays are of different dimensions, I cannot simply add a dimension and collapse them into a single one.
My impression is that Fortran does not allow to convert strings into variable names or something similar, so does anybody know how to deal with this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所要求的本质上是(非常)中的元编程传统的编译语言。你真的不能那样做。
对于 Fortran,我能想到的最接近的方法是找到一个支持 Fortran 2003,并使用其运行时多态性。这样,您可以创建一个父数组类,然后为每个不同的数组边界集创建一个子类,然后循环遍历这些数组。
What you are asking would essentially be metaprogramming in a (very) traditional compiled language. You can't really do that.
The closest I can think of for Fortran would be to get hold of a compiler that supports some of the OO stuff in Fortran 2003, and use its runtime polymorphisim. That way you could create a parent array class, then a subclass for each different set of array bounds, and then loop through an array of those.