I want to convert a character array in Fortran to a numeric type. I think I should avoid strings (perhaps not?), because these arrays store variable length data (user input).
I have the following code:
IMPLICIT NONE
CHARACTER(LEN=1), ALLOCATABLE :: TOKENS(:, :)
CHARACTER(LEN=1) :: TEST_STR(80)
INTEGER :: I
CHARACTER(LEN=80) :: INPUT_STR
INTEGER :: MYINT
INPUT_STR = ' I am a test 12 string '
DO I = 1, LEN(INPUT_STR)
TEST_STR(I) = INPUT_STR(I:I)
END DO
CALL TOKENIZE(TEST_STR, ' ', TOKENS)
PRINT *, "-----------"
INPUT_STR = '15'
PRINT *, INPUT_STR(1:2)
READ(INPUT_STR(1:2), '(I2)') MYINT
PRINT *, MYINT
PRINT *, "-----------"
PRINT *, TOKENS(:, 5)
READ(TOKENS(:, 5), '(I2)') MYINT
PRINT *, MYINT
The output looks like this:
-----------
15
15
-----------
12
1
I have tested separately that the tokenizer presents sane output, which it does. However, it seems that READ(TOKENS(:, 5), '(I2)') MYINT is only reading the first element of TOKENS(:, 5).
As a test, I ran it with READ(TOKENS(1, 5), '(I2)') MYINT and READ(TOKENS(2, 5), '(I2)') MYINT, and it printed 1 and 2 respectively.
Any ideas on how I can get it to read the whole array, rather than just one character at a time?