Nikos Papanikolaou
Nikos Papanikolaou

Reputation: 21

I want to make an array in Fortran 95 with mixed variables

I am new into Fortran and I want to make an array that has both real and characters I tried making a type that lets me have real variables at the first column of the array and characters at the other but it didn't work.The variables I have come from a .txt file. Is there any option to read the variables before getting them in the array or a custom type is the only choice? Thanks in advance!!

The example program is

PROGRAM HOMEWORK 
  IMPLICIT NONE 
  integer::i 
  type custom
    real :: data 
    character :: name 
  end type 
  type (custom), dimension (4) :: AA 

  OPEN(5,FILE="askhsh_fortran.dat") 
  do i=1,4 
    read(5,*) AA(i) % data , AA(i) % name 
  end do 
  WRITE(*,*)AA 
  close (5) 
END PROGRAM HOMEWORK

Upvotes: 1

Views: 740

Answers (1)

M. S. B.
M. S. B.

Reputation: 29391

If you want a variable to have both reals and characters, then you will need to create a custom type, e.g., some code fragments:

type MyType
   real :: data
   character (len=20) :: name
end MyType 

type (MyType), dimension (10) :: table

do i=1, 10
   read (5, *) table (i) % data, table (i) % name
end do

Upvotes: 3

Related Questions