Formatting 2D Arrays in a fortran namelist input file
fortran
Solution
Wow, thanks for the question -- never heard of namelists before :) This is useful!! :) After a little testing, older gfortran versions have a problem with this. Let's say you have
program nltest
implicit none
integer :: a(3,3)
namelist /mylist/ a
a = 0
open(7, file='nlinput.txt')
read(7, nml = mylist)
write(*,*) a
end program nltest
- read a whole array, `a=1,2,3,4,5,6,7,8,9` : this works fine and reads a(1,1), a(2,1), ... as expected regardless of the compiler.
- read an array slice, e.g. `a(2,:)=1,2,3` : this works fine with ifort and gfortran 4.6.1, but with gfortran 4.3 it does not.
So to do what you want you should be able to write `array2d(1,:) = 1,2` if the code is compiled with a recent compiler.
Problem
Im writing a namelist input file for a Fortran code. I know that if you have a 1D array, you can populate a range of elements by, ``` &namelist array(10) = 0, 1, 2, ......., n &END ``` is the equivalent of ``` &namelist array(10) = 0 array(11) = 1 array(12) = 2 ... array(10 + n) = n &END ``` I need to now write a 2d array. I want to do the shortest equivalent to ``` &namelist array2d(1,1) = 1 array2d(1,2) = 2 &END ``` Can I write that as ``` &namelist array2d(1) = 1, 2 &END ``` or do I need to write this as ``` &namelist array2d(1,1) = 1, 2 &END ```