Re: trim(string) problems

From: Bruce (bmp2003a_at_hotmail.com)
Date: 12/26/03


Date: Fri, 26 Dec 2003 02:13:42 GMT


"*** Hendrickson" <***.hendrickson@att.net> wrote in message
news:3FEB8D08.7AA4B0CB@att.net...
>
>
> Bruce wrote:
> >
> > Dear All
> >
> > In trying to troubleshoot a problem I've prepared
> > the test program below. It doesn't reproduce my
> > problem but brings up some other questions.
> >
> > I'm using FTN95 v4.0 on Win XP
> >
> > the line write(*,'(a)')trim(text(n)) will not compile giving the error
> > "TEXT is an assumed size array and cannot appear on an I/O statement"
> >
> > but write()text(n) and write()' '//trim(text(n)) both compile and work
> >
> > Is there something in the standard that causes this or
> > am I looking at a compiler bug?
> >
> > With the problem I was trying to resolve I read a character string
> > from file and outputting with write(*,'(a)')trim(string) gives me an
> > error from using an undefined variable - which it isn't.
> >
> > Changing my output to length=len_trim(string) and
> > write(*,'(a)')string(:length) works fine.
> >
> > Together these make me think it is a compiler problem
> >
> > The output from my program is also shown below.
> >
> > Is there some reason that the program cannot determine
> > size(text) when the subroutine is used from a module
> > but correctly finds size with both explicit and implicit
> > external routines (not that I expected a difference
> > between the implicit and explicit calls but I'm looking
> > for any reason that might cause this)
> >
> > Thanks for any suggestions and
> > Seasons Greetings to all.
> >
> > Bruce
> >
> > !******************************************
> > module mod
> >
> > contains
> >
> > subroutine used_sub(text)
> > implicit none
> > character(len=*),dimension(*), intent(in):: text
> It's kind of a busy day, so I'm nor going to look very hard at this,
> but try DIMENSION(:)
> instead of the (*). The (*) does mean assumed >size< and you can't
> use the array TEXT in any context where the size matters. In
> particular,
> you can't try to take SIZE(TEXT) because there is really no "size"
> available to the compiler. The (*) means you are going to take care
> of all the size related stuff, not ask the compiler. If you use
> (:) the array becomes an assumed >shape< array and the compiler
> will take care of passing the size, etc. information on from the
> caller. Assumed shape is generally the better chioce, but it does
> mean you need an explicit interface. Assumed >size< is mostly a
> FORTRAN 77 holdover.
>

Thanks, that makes my test program with the module work and should
also clear up things in my other program.

I would be interested in anyone's comments about why concatenating
the part that wouldn't compile to a blank string allowed it to compile
and work - or does this simply fall into the category of the compiler
can do whatever it wants with illegal code.

> I don't really understand the error message you are giving. Without
> seeing the actual output, not your interpretation of it, it's possible
> either you or I missed something.

OK, the actual code is below and comments show the error
message and a fix to it - but I don't see why the error occurs

!-----------------------------------------------------------------------
! Show blockda file headers to screen 15 lines at a time
subroutine blockda_showheaders(ida)
!-----------------------------------------------------------------------

implicit none
integer, intent(in):: ida
integer::irec,nread,length
character(len=nrecl):: header ! nrecl is module variable and is direct
access file record length

write(*,'(25(/))')
nread=0

do irec=2,nhead+1
   read(ida,rec=irec)header

   ! following line causes run-time error
   ! "Reference to undefined character (/FULL_UNDEF) "
   write(*,*)trim(header)

   ! following two lines work instead of 1 above
   length=len_trim(header)
   write(*,*)header(:length)

   nread=nread+1
   if(nread==15)then
      write(*,'(t10,a\)')'paused - enter to continue'
      read(*,*)
      nread=0
   end if
end do

write(*,'(t10,a\)')'finished - enter to continue'
read(*,*)
write(*,'(///)')

!------------------------------------------------------
end subroutine blockda_showheaders
!------------------------------------------------------

>
> Anyhow, try changing the (*) to (:) and see what happens.
>
> *** Hendrickson
>
>
> > integer:: n,nn
> >
> > write(*,'(//a)')' Subroutine used_sub (explicit interface (module))'
> > nn=size(text) ; write(*,*)'size(text)=',nn
> > do n=1,nn
> > ! write(*,'(a)')trim(text(n)) ! will not compile
> > ! error : TEXT is an assumed size array and cannot appear on an I/O
> > statement
> > write(*,'(a)')' '//trim(text(n)) ! but will compile like this
> > write(*,'(a)')text(n) ! and this compiles too
> > end do
> >
> > end subroutine used_sub
> > end module mod
> >
> > program test
> > use mod
> >
> > implicit none
> > interface
> > subroutine sub2(t)
> > character(len=*),dimension(*), intent(in):: t
> > end subroutine sub2
> > end interface
> >
> > character*40 text(4)
> >
> > text(1)='Sample text to evaluate problem'
> > text(2)='found in program '
> > text(3)='where trim(char) gives undefined'
> > text(4)='variable error'
> >
> > call used_sub(text)
> > call sub1(text)
> > call sub2(text)
> >
> > end program test
> >
> > !-----------------------------------
> > subroutine sub1(text)
> > !-----------------------------------
> > implicit none
> > character(len=*),dimension(*), intent(in):: text
> > integer:: n,nn
> >
> > write(*,'(//a)')' Subroutine sub1 (implicit interface)'
> > nn=size(text) ; write(*,*)'size(text)=',nn
> > do n=1,nn
> > write(*,'(a)')' '//trim(text(n)) ! as above, compiles like this
> > end do
> >
> > end subroutine sub1
> >
> > !-----------------------------------
> > subroutine sub2(text)
> > !-----------------------------------
> > implicit none
> > character(len=*),dimension(*), intent(in):: text
> > integer:: n,nn
> >
> > write(*,'(//a)')' Subroutine sub2 (explicit interface)'
> > nn=size(text) ; write(*,*)'size(text)=',nn
> > do n=1,nn
> > write(*,'(a)')' '//trim(text(n))
> > end do
> >
> > end subroutine sub2
> >
> > *****************************************************************
> > output:
> >
> > Subroutine used_sub (explicit interface (module))
> > size(text)= 0
> >
> > Subroutine sub1 (implicit interface)
> > size(text)= 4
> > Sample text to evaluate problem
> > found in program
> > where trim(char) gives undefined
> > variable error
> >
> > Subroutine sub2 (explicit interface)
> > size(text)= 4
> > Sample text to evaluate problem
> > found in program
> > where trim(char) gives undefined
> > variable error