# Refactoring a Data statement

**URL:** https://fortran-lang.discourse.group/t/refactoring-a-data-statement/2635
**Category:** Help
**Created:** [January 23, 2022, 10:43am UTC](https://fortran-lang.discourse.group/t/refactoring-a-data-statement/2635 "2022-01-23T10:43:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ashok](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Ashok](https://fortran-lang.discourse.group/u/Ashok)
#### Post date: [January 23, 2022, 10:43am UTC](https://fortran-lang.discourse.group/t/refactoring-a-data-statement/2635/1 "2022-01-23T10:43:14Z")

</div>

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

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

```

and in the code it is used as

```auto
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 ?

---

<div class="post-metadata">

### Author: ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)
#### Post date: [January 23, 2022, 11:29am UTC](https://fortran-lang.discourse.group/t/refactoring-a-data-statement/2635/2 "2022-01-23T11:29:58Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

### Author: ![Ashok](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Ashok](https://fortran-lang.discourse.group/u/Ashok)
#### Post date: [January 23, 2022, 12:03pm UTC](https://fortran-lang.discourse.group/t/refactoring-a-data-statement/2635/3 "2022-01-23T12:03:22Z")

</div>

> [@mecej4](#):
>
> I would leave the DATA statements alone

Is it a good practice ?

---

<div class="post-metadata">

### Author: ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)
#### Post date: [January 23, 2022, 12:52pm UTC](https://fortran-lang.discourse.group/t/refactoring-a-data-statement/2635/4 "2022-01-23T12:52:59Z")

</div>

modern compilers can do the counting

```auto
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…

---

<div class="post-metadata">

### Author: ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)
#### Post date: [January 23, 2022, 1:11pm UTC](https://fortran-lang.discourse.group/t/refactoring-a-data-statement/2635/5 "2022-01-23T13:11:50Z")

</div>

> [@Ashok](#):
>
> … what could be a modern alternative …

@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:

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

```
