I first make a dynamic character array
! whatever I assign to line will be its length
character(len=:), allocatable :: line
then I want to make this a dynamic array of these dynamic character arrays, so refactoring to this:
type(character(len=:)), allocatable, dimension(:) :: lines
allocate(lines(21))
yields this error;
Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
however, if I allocate the array on variable declaration step like so:
type(character(len=:)), allocatable, dimension(21), target :: lines
I get this error:
Allocatable array 'lines' at (1) must have a deferred shape or assumed rank
First off, is this even possible? as languages like python have dynamic string arrays built in and you don't have to think about it, but in fortran things are a little more complicated to get the right kind of string
you need
Secondly, is this a good idea? or is there a fortran practice that I am unaware of to get 21 individual character arrays that have an unknown length. Giving these characters a fixed length will work for my problem like so:
type(character(len=256)), allocatable, dimension(:), target :: lines
allocate(lines(21))
But knowing that I can only have max 256 in my character bothers me for when I get something larger than 256, what happens then? do I lose data?
Thirdly, how can I achieve this goal of a string[]
in fortran? I've exhausted all ideas.
P.S. I am using gfortran version 9.4
Edit:
per the solution by @veryreverie, the code for string[] (jagged array) in fortran looks like this:
type :: stringType
character(len=:), allocatable :: string
end type
type(stringType), allocatable, dimension(:) :: lines
allocate(lines(21))
do i = 1, 21
lines(i)%string = doSomeStringOperation()
end do