将字符串解释为Fortran中的变量名称 [英] Interpret strings as variable names in Fortran

查看:95
本文介绍了将字符串解释为Fortran中的变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问一个名称等于我拥有的字符串的实数变量.这样的事情(我将使示例尽可能简洁):

I'd like to access a real variable with a name equal to a string of characters that I have. Something like this (I'll make the example as clean as possible):

character(len=5) :: some_string
real :: value
value = 100.0
some_string = 'value'

在这一点上,我如何在字符数组 value 和我的真实变量value的名称之间创建关联,以便我可以通过引用字符串<来写100.0的值.code> some_string ?

At this point, how do I create an association between the character array value and the name of my real variable, value, so that I can write the value of 100.0 by referring to the string some_string?

推荐答案

这是我的解决方法:

character(100) :: s
integer        :: val  
val = 100   
write(s,*) val   
print *,trim(s)

这会在屏幕上打印100.有一些我不明白的奇怪之处,但字符s必须很大(他的情况下为100 int).例如,如果您使用3而不是100,则它不起作用.这不是关键的事情,因为使用trim可以解决此问题,但是如果有人可以回答为什么会这样,那就太好了.

This prints 100 to the screen. There is some strangeness which I do not understand however, the character s needs to be very large (100 int his case). For instance, if you use 3 instead of 100, it does not work. This is not a critical thing, as the use of trim fixes this, but it would be nice if somebody could answer why this is the case.

无论哪种方式,这都应该起作用.

Either way, this should work.

这篇关于将字符串解释为Fortran中的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆