评估在Fortran90中派生类型中定义的字符串的特定字母 [英] Evaluating a specific letter of a string defined in a derived type in Fortran90

查看:92
本文介绍了评估在Fortran90中派生类型中定义的字符串的特定字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想评估变量 myline 的第三个字母是否为'C'.

I would like to evaluate if the 3rd letter of variable myline is 'C' or not.

我尝试:

program main

    implicit none

    type line
        integer                     :: count    = 5
        character(len=48)           :: list     = 'ABCDE'
    end type

    type(line)                      :: myline
    character(len=1)                :: letter   = 'C'

    write(*,*) myline%count, myline%list

    if(myline%list(3) == letter) then
        write(*,*) 'TRUE'
    else
        write(*,*) 'FALSE'
    end if

end program

但是我得到了

$ /usr/local/bin/gfortran8 -mcmodel=medium -fcheck=all -Wl,-rpath=/usr/local/lib/gcc8 -o test test.f90
test.f90:15:15:

  if(myline%list(3) == letter) then
               1
Error: Syntax error in IF-expression at (1)
test.f90:17:5:

  else
     1
Error: Unexpected ELSE statement at (1)
test.f90:19:4:

  end if
    1
Error: Expecting END PROGRAM statement at (1)

我正在使用gfortran(gcc8)和Fortran 90标准.

I am using gfortran (gcc8) and the Fortran 90 standard.

推荐答案

在Fortran中,字符子字符串引用始终需要开始和结束位置.因此,您想要的是 myline%list(3:3).

In Fortran, a character substring reference always needs a start and end position. So what you want here is myline%list(3:3).

您可以省略结束位置(保留冒号),例如(3:),这表示字符串的其余部分.同样,您可以省略起始位置,这意味着从第一个字符(:3)开始.

You can omit the end position (retaining the colon), for example (3:), and that means the rest of the string. Similarly you can omit the start position and it means from the first character (:3).

建议,最好使用 parameter 属性声明 letter ,因为它是一个常量,但是您可以使用它.

As a suggestion, letter would be better declared with the parameter attribute as it is a constant, but what you have would work.

这篇关于评估在Fortran90中派生类型中定义的字符串的特定字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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