How to remove all white/blank spaces from a string? 
Author Message
 How to remove all white/blank spaces from a string?

Hi,

Is there a string function in fortran that will remove all white/blank
spaces from a string?  I found ways to remove leading and trailing
blanks but have not yet found a solution for the spaces between words.

Thanks,
Mubin



Wed, 23 Feb 2005 04:30:26 GMT  
 How to remove all white/blank spaces from a string?

Quote:

> Hi,

> Is there a string function in Fortran that will remove all white/blank
> spaces from a string?  I found ways to remove leading and trailing
> blanks but have not yet found a solution for the spaces between words.

I use the attached function which sorta does what you want - you'll still end up with blanks after
the last non-whitespace character, but using a substring (1:n) or a TRIM() will take care of that.

Oh, and it's f90.

cheers,

paulv

p.s. I haven't put the code through too much of a wringer (e.g. not tested with anything to {*filter*}
like zero length strings) in that I don't use it terribly often but it's worked o.k. for what I do
with it.

!------------------------------------------------------------------------------
!S+
! NAME:
!       strcompress
!
! PURPOSE:
!       Function to return a copy of an input string with all whitespace
!       (spaces and tabs) removed.
!
! CATEGORY:
!       Utility
!
! LANGUAGE:
!       Fortran-90
!
! CALLING SEQUENCE:
!       output_string = strcompress( input_string,  &  ! Input
!                                    n              )  ! Output
!
! INPUT ARGUMENTS:
!       input_string:  Character string to be compressed.
!                      UNITS:      None
!                      TYPE:       Character
!                      DIMENSION:  Scalar, LEN = *
!                      ATTRIBUTES: INTENT( IN )
!
! OPTIONAL INPUT ARGUMENTS:
!       None.
!
! OUTPUT ARGUMENTS:
!       n:             Number of useful characters in output string
!                      after compression. From character n+1 -> LEN( input_string )
!                      the output is padded with blanks.
!                      UNITS:      None
!                      TYPE:       Integer
!                      DIMENSION:  Scalar
!                      ATTRIBUTES: INTENT( OUT )
!
! OPTIONAL OUTPUT ARGUMENTS:
!       None.
!
! FUNCTION RESULT:
!       Input character string with internal white space (spaces and tabs)
!       removed. Returned string length is still the length of the input
!       string, but compressed and padded with blanks.
!
! CALLS:
!       None.
!
! SIDE EFFECTS:
!       None.
!
! EXAMPLE:
!       input_string = '  This is a string with spaces in it.'
!       output_string = strcompress( input_string, n )
!       WRITE( *, '( a )' ) '>',output_string( 1:n ),'<'
!   >Thisisastringwithspacesinit.<
!
!       or
!
!       WRITE( *, '( a )' ) '>',TRIM( output_string ),'<'
!   >Thisisastringwithspacesinit.<
!
! PROCEDURE:
!       Definitions of a space and a tab character are made for the
!       ASCII collating sequence. Each single character of the input
!       string is checked against these definitions using the IACHAR()
!       intrinsic. If the input string character DOES NOT correspond
!       to a space or tab, it is not copied to the output string.
!
!       Note that for input that ONLY has spaces or tabs BEFORE the first
!       useful character, the output of this function is the same as the
!       ADJUSTL() instrinsic.
!
! CREATION HISTORY:
!       Written by:     Paul van Delst, CIMSS/SSEC 18-Oct-1999

!S-
!------------------------------------------------------------------------------

  FUNCTION strcompress( input_string, n ) RESULT ( output_string )

    ! -- Arguments
    CHARACTER( * ), INTENT( IN )  :: input_string
    INTEGER,        INTENT( OUT ) :: n

    ! -- Function result
    CHARACTER( LEN( input_string ) ) :: output_string

    ! -- Local parameters
    INTEGER,        PARAMETER :: IACHAR_SPACE = 32, &
                                 IACHAR_TAB   = 9

    ! -- Local variables
    INTEGER :: i
    INTEGER :: iachar_character

    ! -- Initialise output string
    output_string = ' '

    ! -- Initialise output string "useful" length counter
    n = 0

    ! -- Loop over string elements
    DO i = 1, LEN( input_string )

      ! -- Convert the current character to its position
      ! -- in the ASCII collating sequence
      iachar_character = IACHAR( input_string( i:i ) )

      ! -- If the character is NOT a space ' ' or a tab '->|'
      ! -- copy it to the output string.
      IF ( iachar_character /= IACHAR_SPACE .AND. &
           iachar_character /= IACHAR_TAB         ) THEN
        n = n + 1
        output_string( n:n ) = input_string( i:i )
      END IF

    END DO

  END FUNCTION strcompress

--
Paul van Delst

Ph: (301)763-8000 x7274
Fax:(301)763-8545



Wed, 23 Feb 2005 05:45:37 GMT  
 How to remove all white/blank spaces from a string?



Quote:
> Oh, and it's f90.

I was going to recommend an F95 solution as follows:

module eliminate_space
   implicit none
   private
   public strcompress
   character, parameter :: space = achar(32)
   character, parameter :: tab = achar(9)
   character, parameter :: cr = achar(13)
   character, parameter :: lf = achar(10)
   contains
      function strcompress(x)
         character(*), intent(in) :: x
         character(strcompress_len(x)) strcompress
         character y(len(x))
         character z(len(strcompress))

         y = transfer(x, y)
         z = pack(y, y /= space .AND. y /= tab .AND. y /= cr .AND. &
            y /= lf)
         strcompress = transfer(z, strcompress)
      end function strcompress

      pure function strcompress_len(x)
         character(*), intent(in) :: x
         integer strcompress_len
         character y(len(x))

         strcompress_len = count(y /= space .AND. y /= tab .AND. &
            y /= cr .AND. y /= lf)
      end function strcompress_len
end module eliminate_space

program main
   use eliminate_space
   implicit none
   character(1000) a
   integer n

   write(*,'(a)',advance='no') ' Enter the string to be trimmed:> '
   read(*,'(a)',advance='no',size=n) a
   call test(a(1:n))
   contains
      subroutine test(b)
         character(*), intent(in) :: b

         write(*,'(a,a)') ' Input string is: ', b
         write(*,'(a,i0)') ' Length of input string is: ', len(b)
         write(*,'(a,a)') ' Compressed string is: ', strcompress(b)
         write(*,'(a,i0)') ' Length of compressed string is: ', &
            len(strcompress(b))
      end subroutine test
end program main

But both CVF 6.6B and LF95 5.7 died on the above.  Is my code
that {*filter*} compilers?  Are there any that can handle it
correctly?



Wed, 23 Feb 2005 12:38:30 GMT  
 How to remove all white/blank spaces from a string?

Quote:
>Hi,

>Is there a string function in Fortran that will remove all white/blank
>spaces from a string?  I found ways to remove leading and trailing
>blanks but have not yet found a solution for the spaces between words.

>Thanks,
>Mubin


integer :: len_packed
character (len=LLL) :: string
character, dimension (LLL) :: tmparr
equivalence (string, tmparr)

len_packed = size(pack(tmparr, (tmparr /= " ") ))
tmparr(1:len_packed) = pack(tmparr, (tmparr /= " "))
string = string(1:len_packed)                        ! to clean up

Not optimal for performance, but concise.

--

 http://www.ifremer.fr/metocean/group/michel/michel_olagnon.htm
 http://www.fortran-2000.com/



Wed, 23 Feb 2005 16:08:34 GMT  
 How to remove all white/blank spaces from a string?


Quote:
> Hi,

> Is there a string function in Fortran that will remove all white/blank
> spaces from a string?  I found ways to remove leading and trailing
> blanks but have not yet found a solution for the spaces between words.

> Thanks,
> Mubin


Unless I misread some very complex solutions posted so far, none of them
shows what you ask for, that is a function that outputs a string with no
blanks. My function "strip" below does output a string with no blanks.

module test_1
contains
function strip(s1)  result (s2)
character(*) :: s1
character(char_count(s1)) :: s2
integer :: i, n

n = 0
do i = 1,len_trim(s1)
   if (s1(i:i) == ' ') cycle
   n = n+1
   s2(n:n) = s1(i:i)
end do
end function

pure function char_count(s)  result (n)
character(*),intent(in) :: s
integer :: n, i
n = 0
do i = 1,len_trim(s)
   if (s(i:i) == ' ') cycle
   n = n+1
end do
end function
end module test_1

program test
use test_1
integer :: i,n
character(80) :: line = '   abc def  ghij   k   '
write (*,*) strip(line)    ! outputs 11 char string  "abcdefghijk"
end program



Wed, 23 Feb 2005 18:19:21 GMT  
 How to remove all white/blank spaces from a string?


Quote:
> Hi,

> Is there a string function in Fortran that will remove all white/blank
> spaces from a string?  I found ways to remove leading and trailing
> blanks but have not yet found a solution for the spaces between words.

> Thanks,
> Mubin


Below function "strip" is no more complex than solutions already posted,
however it does something they dont,  it generates a exact size output
string which I believe is what you really asked for.

module test_1
contains
function strip(s1)  result (s2)
character(*) :: s1
character(char_count(s1)) :: s2
integer :: i, n, nout

i = 0 ; n = 0 ; nout = char_count(s1)
do
   i = i+1
   if (s1(i:i) == ' ') cycle
   n = n+1
   s2(n:n) = s1(i:i)
   if (n >= nout) exit
end do
end function

pure function char_count(s)  result (n)
character(*),intent(in) :: s
integer :: n, i
n = 0
do i = 1,len_trim(s)
   if (s(i:i) == ' ') cycle
   n = n+1
end do
end function
end module test_1

program test
use test_1
integer :: i,n
character(80) :: line = '   abc def  ghij   k   '
write (*,*) strip(line)    ! outputs 11 char string  "abcdefghijk"
end program



Wed, 23 Feb 2005 18:19:21 GMT  
 How to remove all white/blank spaces from a string?


Quote:
> Hi,

> Is there a string function in Fortran that will remove all white/blank
> spaces from a string?  I found ways to remove leading and trailing
> blanks but have not yet found a solution for the spaces between words.

> Thanks,
> Mubin


I apologize if 2 or more slightly different versions of my reply below
escaped from my newsreader queue, I have deleted them but some may have
progressed beyond my delete key's range..

Unless I misread some the complex solutions posted so far, none of them
shows what you ask for, that is a function that outputs a string with no
blanks. My function "strip" below does output a string with no blanks.

module test_1
contains
function strip(s1)  result (s2)
character(*) :: s1
character(char_count(s1)) :: s2
integer :: i, n

n = 0
do i = 1,len_trim(s1)
   if (s1(i:i) == ' ') cycle
   n = n+1
   s2(n:n) = s1(i:i)
end do
end function

pure function char_count(s)  result (n)
character(*),intent(in) :: s
integer :: n, i
n = 0
do i = 1,len_trim(s)
   if (s(i:i) == ' ') cycle
   n = n+1
end do
end function
end module test_1

program test
use test_1
integer :: i,n
character(80) :: line = '   abc def  ghij   k   '
write (*,*) strip(line)    ! outputs 11 char string  "abcdefghijk"
end program



Wed, 23 Feb 2005 18:26:44 GMT  
 How to remove all white/blank spaces from a string?
It appears my ISP    (central florida roadrunner) refuses to honor my
outlook express delete key for the 1st 2 replies I posted, sorry 'bout that
(it was the first time I have had occasion to try to delete a message, and
it doesnt even work, shucks)..


Wed, 23 Feb 2005 18:44:25 GMT  
 How to remove all white/blank spaces from a string?
Your program sends g95 into an endless loop.
Not bad. You seem to kill a lot of compiler's with it. Can I forward
this to the g95-develop-list so it can be included in the compiler's
testsuite?

- Tobias Schlter


Quote:


> > Oh, and it's f90.

> I was going to recommend an F95 solution as follows:

> module eliminate_space
>    implicit none
>    private
>    public strcompress
>    character, parameter :: space = achar(32)
>    character, parameter :: tab = achar(9)
>    character, parameter :: cr = achar(13)
>    character, parameter :: lf = achar(10)
>    contains
>       function strcompress(x)
>          character(*), intent(in) :: x
>          character(strcompress_len(x)) strcompress
>          character y(len(x))
>          character z(len(strcompress))

>          y = transfer(x, y)
>          z = pack(y, y /= space .AND. y /= tab .AND. y /= cr .AND. &
>             y /= lf)
>          strcompress = transfer(z, strcompress)
>       end function strcompress

>       pure function strcompress_len(x)
>          character(*), intent(in) :: x
>          integer strcompress_len
>          character y(len(x))

>          strcompress_len = count(y /= space .AND. y /= tab .AND. &
>             y /= cr .AND. y /= lf)
>       end function strcompress_len
> end module eliminate_space

> program main
>    use eliminate_space
>    implicit none
>    character(1000) a
>    integer n

>    write(*,'(a)',advance='no') ' Enter the string to be trimmed:> '
>    read(*,'(a)',advance='no',size=n) a
>    call test(a(1:n))
>    contains
>       subroutine test(b)
>          character(*), intent(in) :: b

>          write(*,'(a,a)') ' Input string is: ', b
>          write(*,'(a,i0)') ' Length of input string is: ', len(b)
>          write(*,'(a,a)') ' Compressed string is: ', strcompress(b)
>          write(*,'(a,i0)') ' Length of compressed string is: ', &
>             len(strcompress(b))
>       end subroutine test
> end program main

> But both CVF 6.6B and LF95 5.7 died on the above.  Is my code
> that {*filter*} compilers?  Are there any that can handle it
> correctly?



Wed, 23 Feb 2005 21:45:46 GMT  
 How to remove all white/blank spaces from a string?


Quote:
> Your program sends g95 into an endless loop.
> Not bad. You seem to kill a lot of compiler's with it. Can I forward
> this to the g95-develop-list so it can be included in the compiler's
> testsuite?

Sure.  I would be quite impressed if g95 currently worked correctly
on as much as 25% of the examples I have posted to c.l.f. over the
years.


Wed, 23 Feb 2005 23:33:40 GMT  
 How to remove all white/blank spaces from a string?


Quote:
> I was going to recommend an F95 solution as follows:

Quite an headache! ;-)

I tried to compile it with CVF6.6B, F, and check it with ForCheck.

I managed to compile the version below with F after some alterations.
ForCheck OKs this version too CVF6.6B does not like the len = func( )
stlyle whtever you put there (even intrisics) generate a compile error.

Time for a bug report?

I have a few extra questions on the code as comments.

!-------------------------------------------------------------
module eliminate_space

  implicit none

  ! F does not seam to like a global private...
  private :: strcompress_len
  public  :: strcompress

  intrinsic :: selected_int_kind, char

  integer, public, parameter  :: int = selected_int_kind( 9 )

  ! F does not like achar... Anyway from what I have read it seams that
F2K will
  ! standartize an ANSI so achar and iachar will be redundant. Is this
really like this?
  ! Another thing why the mandatory len=* instead of len=1 in F?
  character( len = * ), private, parameter :: sp = char( 32 )
  character( len = * ), private, parameter :: tb = char( 9 )
  character( len = * ), private, parameter :: cr = char( 13 )
  character( len = * ), private, parameter :: lf = char( 10 )

  contains

  ! I do not understand why this cannot be elemental OK it could return
a vector
  ! in the 'len = ' but if this could happen it would be known at
compile time so
  ! what is the problem?
  pure function strcompress_len( x ) result( lns )

    !intrinsic  :: len, transfer, count
    intrinsic  :: len, count

    character( len = * ),  intent( in )  :: x

    integer( kind = int )  :: lns

    character( len = 1 ), dimension(len(x))  :: y
    integer( kind = int )                    :: i

    ! F does not like transfer neither equivalence so what is left?
    !y = transfer( x, y ) ! You forgot this transfer in the original
code right?
    forall( i = 1:len(x):1 ) y(i) = x(i:i)
    lns = count( y /= sp .AND. y /= tb .AND. y /= cr .AND. y /= lf )

  end function strcompress_len

  pure function strcompress( x ) result( str )

    !intrinsic  :: len, transfer, pack, repeat
    intrinsic  :: len, pack

    character( len = * ),  intent( in )  :: x

    character( len = strcompress_len(x) )  :: str
    !character( len = len(x) )  :: str

    character( len = 1 ), dimension(len(x))   :: y
    character( len = 1 ), dimension(len(str)) :: z
    integer( kind = int )                    :: i

    !y = transfer( x, y )
    forall( i = 1:len(x):1 ) y(i) = x(i:i)
    z = pack(y, y /= sp .AND. y /= tb .AND. y /= cr .AND. y /= lf)
    !str = transfer( z, str )
    forall( i = 1:len(str):1 ) str(i:i) = z(i)

  end function strcompress

end module eliminate_space

program main

  use eliminate_space, only: int, strcompress

  implicit none

  character( len = 1000 )  :: a
  integer( kind = int )    :: n

  write(unit=*,fmt="(a)",advance="no") " Enter the string to be
trimmed:> "
  ! I did not managed to make this read work.
  ! I thinkthe fmt='(a)' makes it read only the full size of the record
or it generates a run
  ! time error. So some other workaround id needed.
  read(unit=*,fmt="(a)",advance="no",size=n) a
  write(unit=*,fmt="(a)") a(1:n)
  call test(a(1:n))

  contains

  subroutine test( b )

    intrinsic :: len, trim, len_trim

    character( len = *), intent( in ) :: b

    write(unit=*,fmt="(a,a)") " Input string is: ", b
    write(unit=*,fmt="(a,i0)") " Length of input string is: ", len(b)
    write(unit=*,fmt="(a,a)") " Compressed string is: ",
trim(strcompress(b))
    write(unit=*,fmt="(a,i0)") " Length of compressed string is: ",
len_trim(strcompress(b))

  end subroutine test

end program main
!-------------------------------------------------------------

Best regards
Jos Rui

--
========================================================================
What is a cynic? A man who knows the price of everything and the value
of nothing. - Oscar Wilde.
========================================================================
iam://Jos Rui Faustino de Sousa http://homepage.esoterica.pt/~jrfsousa/

address://rua Carlos A. Pinto de Abreu no 30C, 1o 3040 Coimbra Portugal
========================================================================



Thu, 24 Feb 2005 06:25:39 GMT  
 How to remove all white/blank spaces from a string?



Quote:
> I managed to compile the version below with F after some alterations.
> ForCheck OKs this version too CVF6.6B does not like the len = func( )
> stlyle whtever you put there (even intrisics) generate a compile error.
> Time for a bug report?

Actually this is more of a len=result style.  Putting a RESULT clause
in there yields an error message that exposes more clearly what CVF
is doing wrong.  I plan on sending Compaq a bug report.

Quote:
>   ! F does not like achar... Anyway from what I have read it seams that
> F2K will
>   ! standartize an ANSI so achar and iachar will be redundant. Is this
> really like this?

ACHAR is so useful that I can't imagine why F doesn't like it.

Quote:
>   ! I do not understand why this cannot be elemental OK it could return
> a vector
>   ! in the 'len = ' but if this could happen it would be known at
> compile time so
>   ! what is the problem?

strcompress_len can be elemental; LF95 has no problem with that,
but strcompress itself can't be because it could then return an
array of character variables with different lengths.

Quote:
>     ! F does not like transfer neither equivalence so what is left?

I think Richard Maine has pointed out that TRANSFER tends to lead
to many compiler bugs.  F was trying to keep things simple.

Quote:
>     !y = transfer( x, y ) ! You forgot this transfer in the original
> code right?

You're my hero, Jos!  I did indeed forget this in the original
code and LF95 gave me a warning about this, but I thought it was
a spurious warning about using y as the MOLD argument in the first
TRANSFER statement in function strcompress (spurious because the
value of MOLD is irrelevant.)  I should have checked more
carefully -- LF95 Express doesn't echo the line that caused the
warning like most other compilers do, thus making it possible
for me to make this mistake.  Inserting this TRANSFER, of course,
causes LF95 5.7 to produce correct results.

Quote:
>     lns = count( y /= sp .AND. y /= tb .AND. y /= cr .AND. y /= lf )

It has subsequently occurred to me that:

character(*), parameter :: set = achar(9)//achar(10)//achar(13)//achar(32)

and subsequently:

lns = count(scan(y,set) == 0)

would have been more elegant.

Quote:
>     !intrinsic  :: len, transfer, pack, repeat

Where did REPEAT come from?

Quote:
>     character( len = strcompress_len(x) )  :: str
>     !character( len = len(x) )  :: str

Had you made this change, the function result would have
had the wrong length.

Quote:
>   ! I did not managed to make this read work.
>   ! I thinkthe fmt='(a)' makes it read only the full size of the record
> or it generates a run
>   ! time error. So some other workaround id needed.
>   read(unit=*,fmt="(a)",advance="no",size=n) a

Perhaps F preconnects default input with PAD = 'NO'.  James
Giles criticized me once for assumimg preconnection with
PAD = 'YES' and now I seem to have been burned -- permission
to dance with glee, James.  A more robust method is to read
the input into some data structure such as a linked list
and then call a subroutine, passing the data structure and
its size so that a local variable can be a character variable
of the correct length.  I posted an example like this but the
code to do all of the above was long enough that i was
criticized for obscuring the intent of my example.  Sometimes
you just can't win.

Well thanks for the discussion and especially for the correction
which supports my impression of LF95 as a particularly robust
compiler.



Thu, 24 Feb 2005 11:36:09 GMT  
 How to remove all white/blank spaces from a string?

Quote:
>   ! F does not like achar... Anyway from what I have read it seams that
> F2K will
>   ! standartize an ANSI so achar and iachar will be redundant. Is this
> really like this?

No, f2k does not require support of ASCII (which is what I assume you
mean instead of ANSI - I used to get those two acronyms mixed up also).

It requires support of all the same printable characters as ascii, but
they do not have to be in the ascii collating order.  It also provides
the option for iso_10646 support, but that isn't mandatory.

I don't recall whether on not EBCDIC, for the obvious real example,
has suitable characters burried away somewhere or not.  I suspect that
an EBCDIC implementation is possible, either because the characters
are there or by a sufficiently liberal interpretation of the standard.
If an EBCDIC implementation of the draft wasn't possible, I'd have thought
someone would have noticed and griped by now.  (Ok, so perhaps I'd have
thought incorrectly - I didn't check myself).

--
Richard Maine
email: my last name at domain
domain: isomedia dot com



Fri, 25 Feb 2005 02:21:24 GMT  
 How to remove all white/blank spaces from a string?


Quote:
> Actually this is more of a len=result style.

I am not sure if I understand but anyway I have conducted some further
tests and it seams that the problem resides in the "double indirection"

like

function foo( bar ) result( baz )

    declaration(type), intent( in )    :: bar

    declaration(type), atttribute(getvalue(bar))    :: baz

    declaration(type), attribute(getvalueother(baz))    :: whatever
!PROBLEM

    ...

This is where the problem resides it also shows if you use integers and
size and gives a much more helpful error:

Error: The function result-name appears in a specification expression.

I have not tested with other combinations but I think this should be the
general picture.

If I got it right I would fell better if someone quoted some standard on
it.

Quote:
> >   ! F does not like achar... Anyway from what I have read it seams
that
> > F2K will
> >   ! standartize an ANSI so achar and iachar will be redundant. Is
this
> > really like this?

Hummm... "standartize an ANSI" should read "standardize in ASCII"

Quote:
> ACHAR is so useful that I can't imagine why F doesn't like it.

If I got it right it should not make any difference ACHAR is ASCII CHAR
and CHAR is processor dependent since most processors use ASCII it
should be the same.

Quote:
> strcompress_len can be elemental; LF95 has no problem with that,

I must have got mixed up somewhere in fact both CVF and F have no
problems with it too.

But ForCheck guives an error for elemental but not for pure:

 **[512 E] illegal subroutine or function reference
From the Manual:

A procedure reference is not allowed in this context.

The function needs an explicit interface and must not be referenced in
this context.

Another bug report?

Quote:
> but strcompress itself can't be because it could then return an
> array of character variables with different lengths.

Yes of course I should have thought of that.

Quote:
> F was trying to keep things simple.

Yes but I would say that TRANSFER qualifies to be part of the "modern"
subset of Fortran...

Quote:
> >     !intrinsic  :: len, transfer, pack, repeat

> Where did REPEAT come from?

I did not cleaned up after myself... ;-)

I was experimenting with fixed lengths and got bitten by pack filling up
the array with garbage.

Quote:
> >     character( len = strcompress_len(x) )  :: str
> >     !character( len = len(x) )  :: str

> Had you made this change, the function result would have
> had the wrong length.

More leftovers...

I was trying to find out if the problem was with your function or
something more broad.

Quote:
> Perhaps F preconnects default input with PAD = 'NO'.

Well that was my first guess but both the CVF documentation and the
Fortran 95 Handbook state that the default is PAD='YES' so I guessed
that the standard behaviour was padding the record and that the problem
laid somewhere else.

Anyway F and CVF have the same behaviour.

Yet another bug report? Or is this some grey area in the standard?

Best regards
Jos Rui

--
========================================================================
I"d lay down my life for two brothers or eight cousins. - John Burdon
Sanderson Haldane.
========================================================================
iam://Jos Rui Faustino de Sousa http://homepage.esoterica.pt/~jrfsousa/

address://rua Carlos A. Pinto de Abreu no 30C, 1o 3040 Coimbra Portugal
========================================================================



Fri, 25 Feb 2005 03:18:34 GMT  
 How to remove all white/blank spaces from a string?



Quote:
> This is where the problem resides it also shows if you use integers and
> size and gives a much more helpful error:
> Error: The function result-name appears in a specification expression.
> I have not tested with other combinations but I think this should be the
> general picture.
> If I got it right I would fell better if someone quoted some standard on
> it.

The appropriate section of the F95 standard seems to be 7.1.6.2:

"A restricted expression is an expression in which each operation
and each primary is
...
(7) A reference to an intrinsic function that is
...
(c) the character inquiry function LEN
...
and where each argument of the function is
...
(b) a variable whose properties inquired about are not
   (i)   dependent on the upper bound of an assumed-size
         array,
   (ii)  defined by an expression that is not a restricted
         expression, or
   (iii) definable by an ALLOCATE or pointer assignment
         statement."

Quote:
> But ForCheck guives an error for elemental but not for pure:
>  **[512 E] illegal subroutine or function reference
> From the Manual:
> A procedure reference is not allowed in this context.
> The function needs an explicit interface and must not be referenced in
> this context.
> Another bug report?

I think ForCheck is wrong here, but I can't see the exact code
which it complained about.  Send them a bug report and see
what they say.

Quote:
> > Perhaps F preconnects default input with PAD = 'NO'.
> Well that was my first guess but both the CVF documentation and the
> Fortran 95 Handbook state that the default is PAD='YES' so I guessed
> that the standard behaviour was padding the record and that the problem
> laid somewhere else.
> Anyway F and CVF have the same behaviour.
> Yet another bug report? Or is this some grey area in the standard?

No.  F and CVF got it right; the bug report will go to Lahey
for not terminating execution at this point.  Section 9.4.3:

"Execution of the program is terminated if ... an end-of record
condition occurs during execution of a nonadvancing READ
statement that contains neither an IOSTAT= specifier nor an
EOR= specifier."

Having fixed this, I have a version that CVF likes now:

module eliminate_space
   implicit none
   private
   public strcompress
   character(*), parameter :: set = achar(9)//achar(10)//achar(13)// &
      achar(32)
   contains
      function strcompress(x)
         character(*), intent(in) :: x
         character(strcompress_len(x)) strcompress
         character y(len(x))
!         character z(len(strcompress))
! Work around CVF bug:
         character z(strcompress_len(x))

         y = transfer(x, y)
         z = pack(y, scan(y, set) == 0)
         strcompress = transfer(z, strcompress)
      end function strcompress

      pure function strcompress_len(x)
         character(*), intent(in) :: x
         integer strcompress_len
         character y(len(x))

         y = transfer(x, y)
         strcompress_len = count(scan(y, set) == 0)
      end function strcompress_len
end module eliminate_space

program main
   use eliminate_space
   implicit none
   character(1000) a
   integer n
   integer iostat

   write(*,'(a)',advance='no') ' Enter the string to be trimmed:> '
! READ statement not required by the standard to stop program:
   read(*,'(a)',advance='no',size=n,IOSTAT=iostat) a
   call test(a(1:n))
   contains
      subroutine test(b)
         character(*), intent(in) :: b

         write(*,'(a,a)') ' Input string is: ', b
         write(*,'(a,i0)') ' Length of input string is: ', len(b)
         write(*,'(a,a)') ' Compressed string is: ', strcompress(b)
         write(*,'(a,i0)') ' Length of compressed string is: ', &
            len(strcompress(b))
      end subroutine test
end program main



Fri, 25 Feb 2005 16:19:01 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. removing leading white space?

2. removing excess white space?

3. blank lines/lines with spaces (how to remove)

4. Delete White Space in Strings

5. remove blank lines before lines beginning with string

6. how to remove trailing blanks of a string?

7. splitting a string on blank spaces

8. removing spaces from string

9. removing trailing spaces from string variables

10. Removing Extra Spaces From A String

11. help! remove embedded spaces from a string

12. How to remove spaces from a string ?

 

 
Powered by phpBB® Forum Software