Re: trim(string) problems
From: *** Hendrickson (***.hendrickson_at_att.net)
Date: 12/26/03
- Next message: West Coast Engineering: "Santa Claus uses Fortran"
- Previous message: Bruce: "trim(string) problems"
- In reply to: Bruce: "trim(string) problems"
- Next in thread: Bruce: "Re: trim(string) problems"
- Reply: Bruce: "Re: trim(string) problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 26 Dec 2003 01:20:09 GMT
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.
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.
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
- Next message: West Coast Engineering: "Santa Claus uses Fortran"
- Previous message: Bruce: "trim(string) problems"
- In reply to: Bruce: "trim(string) problems"
- Next in thread: Bruce: "Re: trim(string) problems"
- Reply: Bruce: "Re: trim(string) problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]