Amzocks
Amzocks

Reputation: 123

DATA Statement issue : Not enough variables

I'm facing difficulties to figure out why my code is giving me this error

error 281 - Not enough variables in DATA statement

I am using the latest Silverfrost on Windows 8. The relevant piece of my module is,

...
INTEGER, parameter :: maxExampleTypes     =   5
TYPE ExampleInfo
    CHARACTER (len=50) :: ExDeckName
    INTEGER A(maxExampleTypes)
    INTEGER ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)
DATA Example(1)%ExDeckName/'test.dck'/
DATA Example(1)%A/1,2,3,4,5/
...

Curiously, when I only specify one variable for A with

DATA Example(1)%A/1/

the error disappears.

Have you got any idea where it could come from?

Upvotes: 2

Views: 337

Answers (1)

Alexander Vogt
Alexander Vogt

Reputation: 18098

I would never use the DATA statement in modern Fortran. Try

...
INTEGER, parameter :: maxExampleTypes     =   5
TYPE ExampleInfo
    CHARACTER (len=50) :: ExDeckName
    INTEGER            :: A(maxExampleTypes)
    INTEGER            :: ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)

Example(1)%ExDeckName = 'test.dck'
Example(1)%A          = (/ 1,2,3,4,5 /)
...

If the values are supposed to be default values, put them into the type declaration:

...
INTEGER, parameter :: maxExampleTypes     =   5
TYPE ExampleInfo
    CHARACTER (len=50) :: ExDeckName = 'test.dck'
    INTEGER            :: A(maxExampleTypes) = (/ 1,2,3,4,5 /)
    INTEGER            :: ExUnits
ENDTYPE ExampleInfo
TYPE(ExampleInfo) :: Example(10)
...

Sample test program:

module testmod
  implicit none
  INTEGER, parameter :: maxExampleTypes     =   5
  ! Type with default values
  TYPE ExampleInfo
      CHARACTER (len=50) :: ExDeckName = 'test.dck'
      INTEGER            :: A(maxExampleTypes)= (/ 1,2,3,4,5 /)
      INTEGER            :: ExUnits
  ENDTYPE ExampleInfo

contains
  subroutine init_ExampleInfo(array)
    implicit none
    type(ExampleInfo), intent(out):: array(:)
    integer                       :: i

    do i=1,size(array)
      array(i)%ExDeckName = 'test.dck'
      array(i)%A          = (/ 1,2,3,4,5 /)
    enddo
  end subroutine
end module

program test
  use testmod
  implicit none
  TYPE(ExampleInfo) :: Example(10)

  ! Initialize manually
  ! call init_ExampleInfo(Example)
  write(*,*) Example(1)%ExDeckName, Example(1)%A

  ! Set new values
  Example(1)%ExDeckName = 'test2.dck'
  Example(1)%A          = (/ 5,4,3,2,1 /)

  write(*,*) Example(1)%ExDeckName, Example(1)%A
end program

Upvotes: 2

Related Questions