Array of Strings in Fortran 77

Siu picture Siu · Apr 7, 2009 · Viewed 16.3k times · Source

I've a question about Fortran 77 and I've not been able to find a solution.

I'm trying to store an array of strings defined as the following:

character matname(255)*255

Wich is an array of 255 strings of length 255.

Later I read the list of names from a file and I set the content of the array like this:

matname(matcount) = mname

EDIT: Actually mname value is harcoded as mname = 'AIR' of type character*255, it is a parameter of a function matadd() wich executes the previous line. But this is only for testing, in the future it will be read from a file.

Later on I want to print it with:

write(*,*) matname(matidx)

But it seems to print all the 255 characters, it prints the string I assigned and a lot of garbage.

  • So that is my question, how can I know the length of the string stored?
  • Should I have another array with all the lengths?
  • And how can I know the length of the string read?

Thanks.

Answer

Timotei picture Timotei · Apr 7, 2009

You can use this function to get the length (without blank tail)

integer function strlen(st)
      integer       i
      character     st*(*)
      i = len(st)
      do while (st(i:i) .eq. ' ')
        i = i - 1
      enddo
      strlen = i
      return
      end

Got from here: http://www.ibiblio.org/pub/languages/fortran/ch2-13.html

PS: When you say: matname(matidx) it gets the whole string(256) chars... so that is your string plus blanks or garbage