如何将字符数组传递到字符串 [英] How to pass character array into string

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

问题描述

我想知道我怎么能从一个字符数组到几个字符串。的确,我有一个包含17个文件路径的字符数组。
可以说:

 字符,维(29,17):: FILE_SIM_all 
字符,长度29):: FILE_SIM

!声明结束

FILE_SIM_all(1:29,1)=/Users/toto/Documents/toto.nc
FILE_SIM_all(1:29,2)=等...

我想递归地转换(在sim = 1,17的for循环中)sim行FILE_SIM_all到一个字符串。让我们说一些类似于:

  do sim = 1,17 
FILE_SIM(1:29)= FILE_SIM_all(1:29 ,sim)
enddo

但是在编译我的程序时出现以下错误:


错误#6366:数组表达式的形状不一致。 [FILE_SIM]


我做错了什么?感谢!

解决方案

从一个更简单的问题变体开始,从一个一级数组创建一定长度的字符标量你可以使用一个赋值语句。

 !声明矢量为
的十个大小的第一个数组!长度为一个字符。
CHARACTER(1):: vector(10)
!声明标量是长度为十的字符标量,
!所以LEN(标量)== SIZE(矢量)
字符(10)::标量
INTEGER :: i!循环索引。

!定义`vector`。请注意,
!的右侧赋值是一个长度为一个字符的排列一个数组,
!与矢量的定义一致。
vector =(/'a','b','c','d','e','f','g','h','i','j'/)

!遍历`vector`元素和
的字符! `标量'并转移个别字符。
DO i = 1,LEN(标量)!或大小(矢量)
标量(i:i)=向量(i)
END DO

(FORALL语句可能更简洁一些,尤其是在F2008中。)



你的问题只是给上面添加了另一个等级。

 !声明`矩阵'是
形状(10,2)的一个二级数​​组!长度为一个字符。
CHARACTER(1)::`matrix`(10,2)
!声明`list`是大小为2和
的排名第一的数组!长度十,所以LEN(列表)== SIZE(矩阵,1)和
! SIZE(list)== SIZE(matrix,2)
CHARACTER(10):: list(2)
INTEGER :: i!内环指标。
INTEGER :: j!外环索引。

!定义`矩阵'。请注意,每个
的右侧!赋值是一个长度为一个字符的排列一个数组,
!与矩阵列的定义一致。
矩阵(:,1)=(/'a','b','c','d','e','f','g','h','i',' (',2)=(/'0','1','2','3','4','5','6','7',' 8','9'/)

!遍历矩阵的列和列表的元素。
DO j = 1,SIZE(列表)!或SIZE(矩阵,1)
!循环矩阵的行和
的字符! list的一个元素,并传输单个字符。
DO i = 1,LEN(列表)!或者SIZE(矩阵,2)
list(j)(i:i)=矩阵(i,j)
END DO
END DO

请注意,Fortran中的标量与数组非常不同。如果将一个标量赋给一个数组,则将该标量的值赋值给数组中的每个元素,就好像您写了 arrary(1)=标量;数组(2)=标量; ...



还要注意,如果右手边的长度与长度不匹配,则内在字符分配会截断(或打击垫)因此在你的代码中:

  FILE_SIM_all 

(1:29,1)=/Users/toto/Documents/toto.nc

其中将一个标量赋给一个数组部分,除非需要29个单斜杠字符,否则不会做任何有用的操作!



示例中的错误信息出现是因为您要分配一个大小为29的数组部分到一个标量(恰好是长度为29的字符对象)。一般来说,您不能将数组(等级为1或更大)分配给标量(等级为零)。


I am wondering how could I go from a character array to several character strings. Indeed, I have a character array containing 17 file path. Lets say :

character, dimension(29,17) :: FILE_SIM_all
character, length(29) :: FILE_SIM

! Declarations end

FILE_SIM_all(1:29,1) = "/Users/toto/Documents/toto.nc"
FILE_SIM_all(1:29,2) = etc...

I would like to convert recursively (inside a for loop with sim=1,17) the "sim" row of FILE_SIM_all to a character string. Lets say something like

do sim=1,17
    FILE_SIM(1:29) = FILE_SIM_all(1:29,sim)
enddo

But I'm getting the following error when compilling my program :

error #6366: The shapes of the array expressions do not conform. [FILE_SIM]

What am I doing wrong? Thanks !

解决方案

Starting with a simpler variant of the problem, to create a character scalar of a certain length from an rank one array of length one characters that has the same size as the certain length, you can use an assignment statement.

! Declare vector to be an rank one array of size ten of 
! length one characters.
CHARACTER(1) :: vector(10)
! Declare scalar to be a character scalar of length ten, 
! so LEN(scalar) == SIZE(vector)
CHARACTER(10) :: scalar
INTEGER :: i     ! Loop index.

! Define `vector`.  Note that the right hand side of the 
! assignment is a rank one array of ten length one characters, 
! consistent with the definition of vector.
vector = (/ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' /)

! Loop over the elements of `vector` and the characters of 
! `scalar` and transfer the individual characters.
DO i = 1, LEN(scalar)  ! or SIZE(vector)
  scalar(i:i) = vector(i)
END DO

(A FORALL statement might be a little more concise, particularly in F2008.)

Your problem simply adds another rank to the above.

! Declare `matrix` to be an rank two array of shape (10,2) of 
! length one characters.
CHARACTER(1) :: `matrix`(10,2)
! Declare `list` to be a rank one array of size 2 and 
! length ten, so LEN(list) == SIZE(matrix,1) and 
! SIZE(list) == SIZE(matrix,2)
CHARACTER(10) :: list(2)
INTEGER :: i     ! Inner Loop index.
INTEGER :: j     ! Outer loop index.

! Define `matrix`.  Note that the right hand side of each 
! assignment is a rank one array of ten length one characters, 
! consistent with the definition of a column of matrix.
matrix(:,1) = (/ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' /)
matrix(:,2) = (/ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' /)

! Loop over the columns of matrix and the elements of list.
DO j = 1, SIZE(list)   ! or SIZE(matrix,1)
  ! Loop over the rows of `matrix` and the characters of 
  ! an element of `list` and transfer the individual characters.
  DO i = 1, LEN(list)   ! or SIZE(matrix,2)
    list(j)(i:i) = matrix(i,j)
  END DO
END DO

Note that a scalar in Fortran is very distinct from an array. If you assign a scalar to an array, you assign the value of that scalar to every element in the array, as if you had written arrary(1) = scalar ; array(2) = scalar ; ...

Note also that intrinsic character assignment truncates (or pads) if the length of the right hand side does not match the length of the left hand side.

Hence in your code:

FILE_SIM_all(1:29,1) = "/Users/toto/Documents/toto.nc"

which assigns a scalar to an array section, does not do anything useful, unless you want 29 single slash characters!

The error message in your example arises because you are trying to assign an array section of size 29 to a scalar (that happens to be a character object of length 29). In general, you cannot assign arrays (rank of one or greater) to scalars (rank of zero).

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

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