I am using GNU Fortran (GCC) 4.8.2
I want to read allocatable arrays from a namelist. But I don't know in advance how many elements have to be read into the allocatable array, so I cannot allocate it before to read the namelist.
This is my namelist: namelist.nml:
&SECTION_1
intList = 5,6,7
/ ! End of SECTION_1
And this is my program: namelist.f08:
program namelist
implicit none
integer, allocatable :: intList(:)
integer :: U ! Unit to read the namelist file
namelist /SECTION_1/ intList
!allocate(intList(3)) ! <-- If I uncomment this, the program works.
open(NEWUNIT=U, file="namelist.nml", status='OLD', recl=80, delim='APOSTROPHE')
rewind(U)
read(U, nml=SECTION_1)
close(U)
write (*,*) intList
end program namelist
If I uncomment the labeled line, the program works, but, as I said before, I cannot allocate before to read the namelist. Does anybody know how to accomplish this?.
&END
indicates the start of namelist input for a namelist named END. You use a slash character to terminate namelist input. I am a little surprised that the processor doesn't complain at runtime about that. Note that you can read in arbitrary length namelist input using Fortran 2003's UDDTIO feature, but that would require gfortran >= 7.0. – Scrip