Refactoring a Data statement

Hi,
I have a F77 code in which there is a DATA statement like this

 DATA TOP/'FRAME ANALYSIS','PLANE STRAIN ANALYSIS','BUCKLING ANALYSIS',
                    'SHELL ANALYSIS'/

and in the code it is used as

TOP(i)

I think it is good data structure - array of strings. But what could be a modern alternative ? I think “DATA” blocks should be avoided so how do I replace this ?

I would leave the DATA statements alone, unless they are an irritant. In that case, you could write:

program banData
implicit none
character(21), parameter :: TOP(4) = &
   ['FRAME ANALYSIS       ', &
    'PLANE STRAIN ANALYSIS', &
    'BUCKLING ANALYSIS    ', &
    'SHELL ANALYSIS       ']
integer i
print '(i2,2x,A)',(i,top(i),i=1,size(top))
end program
1 Like

Is it a good practice ?

modern compilers can do the counting

program banData
implicit none
character(*), parameter :: TOP(*) = &
   ['FRAME ANALYSIS       ', &
    'PLANE STRAIN ANALYSIS', &
    'BUCKLING ANALYSIS    ', &
    'SHELL ANALYSIS       ']
integer i
print '(i2,2x,A)',(i,top(i),i=1,size(top))
end program

which is (according to my knowledge) not possible for DATA.
The drawback is the enforced padding…

2 Likes

@Ashok , as shown by the other posts here, the “modern alternative” is clearly named constants and letting the compiler do as much as possible for setting up the constant(s), including the size of the array i.e., use the implied-shape facility from Fortran 2018 revision.

In the case of arrays of named constants, one may see a value in type-declaration within the array constructor on the right, especially with arrays of string literals (CHARACTER type) that can obviate the need to individually pad each array element, see below:

! 'xx' is some suitable length, say >= 21
character(len=*), parameter :: TOP(*) = [ character(len=xx) ::  &
    'FRAME ANALYSIS',                                           &
    'PLANE STRAIN ANALYSIS',                                    &
    'BUCKLING ANALYSIS',                                        &
    'SHELL ANALYSIS' ]
3 Likes