문제의 간단한 변형으로 시작하여 특정 길이와 동일한 크기를 가진 길이가 1자인 순위 배열에서 특정 길이의 문자 스칼라를 만들려면 할당 문을 사용할 수 있습니다.
! 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 문은 특히 F2008에, 좀 더 간결 수 있습니다.)
당신의 문제는 단순히 위의 또 다른 순위를 추가합니다.
! 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
포트란의 스칼라는 배열과 매우 다릅니다. 배열에 스칼라를 할당하면 배열의 모든 요소에 해당 스칼라 값을 할당합니다. arrary(1) = scalar ; array(2) = scalar ; ...
오른쪽의 길이가 자르면 내재 문자 할당이 잘립니다. 왼손 쪽 길이가 왼손 쪽 길이와 일치하지 않습니다. 코드에 따라서
: 배열 섹션에 스칼라를 할당
FILE_SIM_all(1:29,1) = "/Users/toto/Documents/toto.nc"
가 유용 아무것도하지 않습니다, 당신은 29 개 단일 슬래시 문자를하지 않으려면!
예에서 오류 메시지는 크기 29의 배열 섹션을 스칼라 (길이 29의 문자 개체가 됨)에 할당하려고하기 때문에 발생합니다. 일반적으로 스칼라 (순위 0)에 배열 (1 이상의 순위)을 지정할 수 없습니다.