Fortran 90 中循环变量名

发布于 2024-09-18 05:17:48 字数 256 浏览 1 评论 0原文

我有大量不同维度的数组。不过,我按顺序命名它们,例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-09-25 05:17:48

您所要求的本质上是(非常)中的元编程传统的编译语言。你真的不能那样做。

对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文