提取Fortran字符串数组的子字符串 [英] Extract substring of Fortran string array

查看:655
本文介绍了提取Fortran字符串数组的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何提取Fortran字符串数组的子字符串?例如

program testcharindex
    implicit none
    character(len=10), dimension(5) :: s
    character(len=10), allocatable :: comp(:)
    integer, allocatable  :: i(:), n(:)
    s = (/ '1_E ', '2_S ', '3_E ', '14_E', '25_S' /)
    i = index(s,'_')
    print *,' i = ', i
    n = s(1:i-1) ! or n = s(:i-1)
    comp = s(i+1:)
    print *,' n = ', n
    print *,' comp = ', comp
end program

使用gfortran编译会产生错误:

testcharindex.f90:11:10:

n = s(1:i-1) 1个 错误:(1)处的数组索引必须为标量

有什么办法可以避免do循环吗?如果可以提取一个字符串数组的索引,那么我希望人们应该能够提取一个动态定义的字符串数组的子字符串(无需遍历数组元素).我太乐观了吗?

解决方案

您在这里遇到了几个问题.其中一个很容易解决(并且还有其他问题:您可以找到更多详细信息).

1

n = s(1:i-1)

编译器抱怨的

是尝试引用数组s的一部分,而不是引用数组s的元素的子字符串数组.要访问数组的子字符串,您将需要

n = s(:)(1:i-1)

但是,这与您的第二个问题有关.由于编译器抱怨访问数组部分,因此i必须是标量.对于访问数组的子字符串的情况也是如此.上面的行仍然无效.

本质上,如果要访问数组的子字符串,则每个子字符串必须具有完全相同的结构.也就是说,在s(:)(i:j)中,ij都必须是标量整数表达式.这是因为希望返回数组的每个元素都具有相同的长度.

那么,您将需要使用循环.


1 正如Performance Performance Mark曾经评论过的那样,作业本身也存在问题.我只考虑了右侧的表达.即使针对有效的数组子字符串进行了校正,该表达式仍然是一个字符数组,无法根据需要将其分配给整数标量n.

如果您想要有关选择子字符串的字面答案,请阅读上述内容.如果您只关心将字符数组的一部分转换为整数数组",那么另一个答案可以很好地解决问题. >

How does one extract a substring of a Fortran string array? For example

program testcharindex
    implicit none
    character(len=10), dimension(5) :: s
    character(len=10), allocatable :: comp(:)
    integer, allocatable  :: i(:), n(:)
    s = (/ '1_E ', '2_S ', '3_E ', '14_E', '25_S' /)
    i = index(s,'_')
    print *,' i = ', i
    n = s(1:i-1) ! or n = s(:i-1)
    comp = s(i+1:)
    print *,' n = ', n
    print *,' comp = ', comp
end program

Compiling with gfortran yields the error:

testcharindex.f90:11:10:

n = s(1:i-1) 1 Error: Array index at (1) must be scalar

Is there any way of avoiding a do loop here? If one can extract an index of a string array, I would expect that one should be able to extract a dynamically-defined substring of a string array (without looping over the array elements). Am I too optimistic?

解决方案

You have a couple of problems here. One of which is easily addressed (and has been in other questions: you can find these for more detail).

The line1

n = s(1:i-1)

about which the compiler is complaining is an attempt to reference a section of the array s, not an array of substrings of elements of array s. To access the substrings of the array you will need

n = s(:)(1:i-1)

However, this is related to your second problem. As the compiler complains for accessing the array section, the i must be a scalar. This is also true for the case of accessing substrings of an array. The above line will still not work.

Essentially, if you wish to access substrings of an array, each substring has to have exactly the same structure. That is, in s(:)(i:j) both i and j must be scalar integer expressions. This is motived by the desire to have every element of the returned array being the same length.

You will, then, need to use a loop.


1 As High Performance Mark once commented, there's also a problem with the assignment itself. I considered simply the expression on the right-hand side. Even corrected for a valid array substring, the expression is still a character array, which cannot be assigned to the integer scalar n as desired.

If you want the literal answer about selecting substrings then read as above. If you simply care about "converting part of a character array to an integer array" then another answer covers things well.

这篇关于提取Fortran字符串数组的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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