Frederico Arez
Frederico Arez

Reputation: 19

Rank mismatch in array reference when reading data from a table

I have a text file with the following format (11 columns and 8 rows)

Name    Chris    Kyle    Peter    Luke    Sally (...)   
age     xxxx    xxxx    xxxx    xxxx    xxxx
height          xxxx    xxxx    xxxx    xxxx
weight          xxxx    xxxx    xxxx    xxxx
class           xxxx    xxxx    xxxx    xxxx

The first column (chris) only has data in the first row.

I need to build a program to read this table, this is what I have so far but I can't get it to work...

  program readtable

  implicit none
  integer :: i, j, num_col, num_row
  double precision, dimension (2) :: a
  character(14), dimension (1) :: variable

  num_col = 11
  num_row = 8

  open(100,file='SSL.dat',status='old')
  do j=1, num_row

        read(100,*) variable(j), (a(i,j), i=1,num_col)
        print*, variable(j), a(i,j)

  end do

  end program

When I run this piece of code I get a rank mismatch.

I want to read the data in this table so that I can then assign the data to specific variables. For instance, If I want to do some stuff with kyle and Peter I know that for Kyle a(:,2) and for Peter a(:,3)

Here is the error I get when I compile with gfortran as a Fortran 90 code

               read(100,*) variable(j), (a(i,j),i=1,num_col))
                                         1
Error: Rank mismatch in array reference at (1) (2/1)

Upvotes: 0

Views: 1694

Answers (1)

It looks like you assume that dimension(n) makes the array n-dimensional. That is not true! It make s the array one-dimensional and with shape from 1 to n, i.e., (1:n).

If you want to declare a two-dimensional array with 8 rows and 11 columns you have to do:

double precision, dimension(8,11) :: a

or equivalently

double precision :: a(8,11)

Similarly, variable must be something like:

character(14), dimension(8) :: variable

This will not make your program complete and functional, but will fix the immediate issue the compiler is complaining about. Please keep your questions narrow.

Upvotes: 1

Related Questions