数组构造函数中不同的CHARACTER长度(3/4),如何修剪字符串 - fortran [英] Different CHARACTER lengths (3/4) in array constructor, how to trim strings - fortran

查看:980
本文介绍了数组构造函数中不同的CHARACTER长度(3/4),如何修剪字符串 - fortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据对类似问题的回答,我已经声明了这些字符 gfortran不允许具有不同组件长度的字符数组。不过,我想使用修剪功能,因为我需要添加空格来手动填充名称,然后这些变量在代码的另一部分中使用。我可以在创建数组的同时进行修剪吗?


错误:数组构造函数中不同CHARACTER长度(3/4) )

如果我添加随机字符以使它们具有相同的长度,但由于显而易见的原因,我无法这么做。我用gfortran和mpif90编译了相同的结果。

 使用mod_maxdims,仅:maxstr 
integer,parameter :: nvars_ncep = 12

character(len = maxstr),parameter,dimension(nvars_ncep):: vars_ncep =&
(/'air'&!空气温度[K]
,'pres'&!压力[Pa]
,'rhum'&!相对湿度[%]
,'uwnd'&!纬向风[m / s]
,'vwnd'&!纬向风[m / s]
,'pres'&!压力[Pa]
,'dlwrf'&!向下长波辐射[W / m2]
,'nbdsf'&!近红外光束辐射[W / m2]
,'nddsf'& !近红外漫射辐射[W / m2]
,'vbdsf'& !可见光束辐射[W / m2]
,'vddsf'& !可见光束辐射[W / m2]
,'prate'/)!降水率[kg / m2 / s]


解决方案

gfortran is防止您编写非标准代码;这是禁止它的语言标准,而不是实现。



如果您按照您所做的初始化字符数组,则所有条目必须具有相同的长度。在你的情况下,你必须用足够的空格填充每个较短的条目,以使它们全部相等。

另一种方法是在程序执行开始时插入条目。如果你写了 vars_ncep(1)='air'之类的东西,那么附加字符将被设置为空格,编译器会为你处理。然而,这意味着你的数组不能是参数


According to an answer to a similar question, I have declared the characters as indicated here gfortran does not allow character arrays with varying component lengths . However I would like to use a trim function because I need to add spaces to manually pad the names and these variables are then used in another part of the code. Can I trim at the same time as creating the array?

Error: Different CHARACTER lengths (3/4) in array constructor at (1)

If I add random characters to make them the same length it works but I can't do that for obvious reasons. I have compiled both with gfortran and mpif90 with same results

use mod_maxdims , only : maxstr
integer, parameter :: nvars_ncep = 12

character(len=maxstr), parameter, dimension(nvars_ncep) :: vars_ncep =                  &
                           (/ 'air'              & ! Air temperature                  [      K]
                            , 'pres'             & ! Pressure                         [     Pa]
                            , 'rhum'             & ! Relative humidity                [      %]
                            , 'uwnd'             & ! Zonal wind                       [    m/s]
                            , 'vwnd'             & ! Zonal wind                       [    m/s]
                            , 'pres'             & ! Pressure                         [     Pa]
                            , 'dlwrf'            & ! Downward long wave radiation     [   W/m2]
                            , 'nbdsf'            & ! Near-IR beam radiation           [   W/m2]
                            , 'nddsf'            & ! Near-IR diffuse radiation        [   W/m2]
                            , 'vbdsf'            & ! Visible beam radiation           [   W/m2]
                            , 'vddsf'            & ! Visible beam radiation           [   W/m2]
                            , 'prate'           /) ! Precipitation rate               [kg/m2/s]

解决方案

gfortran is preventing you from writing non-standard code; it's the language standard which forbids it, not the implementation.

If you initialise a character array as you have done then all the entries must have the same length. In your case you would have to pad each shorter entry with enough spaces to make them all equally long.

The alternative would be to insert the entries when program execution starts. If you write something like vars_ncep(1) = 'air' then the additional characters will be set to spaces, the compiler will take care of that for you. This however, would mean that your array could not be a parameter.

这篇关于数组构造函数中不同的CHARACTER长度(3/4),如何修剪字符串 - fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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