How to pass an array of characters to a string

I am wondering how I can go from an array of characters to multiple character strings. In fact, I have an array of characters containing 17 paths to the file. Let's 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 string "sim" FILE_SIM_all into a character string. Let's say something like

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

But when compiling my program, I get the following error:

error # 6366: Forms of array expressions do not match. [FILE_SIM]

What am I doing wrong? Thanks!

+3
source share
1 answer

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

 ! 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 

(The FORALL statement may be a bit more concise, especially in F2008.)

Your problem just 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 different from an array. If you assign a scalar to an array, you assign the value of this scalar to each element of the array, as if you wrote arrary(1) = scalar ; array(2) = scalar ; ... arrary(1) = scalar ; array(2) = scalar ; ...

Also note that the internal character assignment truncates (or gaskets) if the length of the right side does not match the length of the left side.

Therefore, in your code:

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

which assigns a scalar to an array does nothing useful if you don't want to use 29 single slash characters!

The error message in your example is due to the fact that you are trying to assign an array of size 29 to a scalar (which is a character object of length 29). In general, you cannot assign arrays (the rank of one or more) to scalars (the rank of zero).

+4
source

Source: https://habr.com/ru/post/1268627/


All Articles