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

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

问题描述

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

程序 testcharindex隐式无字符(len=10),维度(5)::s字符(len=10),可分配的 :: comp(:)整数,可分配 :: i(:), n(:)s = (/'1_E', '2_S', '3_E', '14_E', '25_S'/)i = 索引(s,'_')打印*,'我=',我n = s(1:i-1) !或 n = s(:i-1)comp = s(i+1:)打印*,'n =',n打印*,'comp =',comp结束程序

使用 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 正如高性能标记曾经评论的那样,作业本身也存在问题.我只考虑了右侧的表达式.即使针对有效的数组子字符串进行了纠正,表达式仍然是字符数组,无法根据需要将其分配给整数标量 n.

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

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天全站免登陆