how to remove trailing blanks of a string? 
Author Message
 how to remove trailing blanks of a string?

hi, there,

i am using g77.

i am trying to remove some trailing blanks of a string. is there some
built in functions that can be used?

for example,

character*15 test_string
test_string = 'temp'
open(10,file=test_string//'.dat',status='new')

i will end up with temp       .dat

one way i can think of is that when declare test_string, do not setup
the length of string. can fortran automaticlly set up a string w/o a
length?

or use a function to remove the trailing blanks.

could someone tell me how to do?

thank you very much.

richard



Mon, 12 Jul 2004 16:24:23 GMT  
 how to remove trailing blanks of a string?
open(10,file=trim(test_string) // '.dat',status='new')  ! opens file "temp.dat"


Mon, 12 Jul 2004 16:38:56 GMT  
 how to remove trailing blanks of a string?
"trim()" is a f90 function.
Quote:

> open(10,file=trim(test_string) // '.dat',status='new')  ! opens file "temp.dat"



Mon, 12 Jul 2004 21:26:50 GMT  
 how to remove trailing blanks of a string?

Quote:

> hi, there,

> i am using g77.

> i am trying to remove some trailing blanks of a string. is there some
> built in functions that can be used?

> for example,

> character*15 test_string
> test_string = 'temp'
> open(10,file=test_string//'.dat',status='new')

> i will end up with temp       .dat

> one way i can think of is that when declare test_string, do not setup
> the length of string. can fortran automaticlly set up a string w/o a
> length?

> or use a function to remove the trailing blanks.

> could someone tell me how to do?

Hmm, apart from checking if len_trim() (or trim() for that matter) is accepted in g77, you
might try:

  character*15 test_string
  integer i, length

  test_string = 'temp'

  length = 0
  DO i = 1, LEN( test_string )
    IF ( test_string(i:i) .NE. ' ' ) THEN
      length = length + 1
    END IF
  END DO

  OPEN(10,file=test_string(1:length)//'.dat',status='new')

It is a bit longwinded and it does rely on the fact that everything else in the string will be
a space (which may or may not be the correct behaviour - I don't know what the standard
dictates in that respect). I would suggest sticking this in it's own function and use it
similarly to the f90 LEN_TRIM().

Also, in the past I have found that passing strings into routines, sticking stuff in them and
then passing them back sometimes filled the unsed/unassigned bits of the string with random
{*filter*}so I got into the habit of "zeroing out" strings with spaces before I assigned them.
Probably not an issue anymore, but you never know.

Alternatively, if you can get your hands on a f90/95 compiler you can do what David Frank
suggested with the TRIM() intrinsic in another post. Also, I _believe_ the f90 standard
dictates that the unsed/unassigned parts of string be filled with blanks (based on reading
"Fortran 90/95 Explained"
by M. Metcalf and John Reid).

cheers,

paulv

--
Paul van Delst             Religious and cultural

Ph: (301)763-8000 x7274    fantasy
Fax:(301)763-8545                   V.S.Naipaul



Mon, 12 Jul 2004 22:34:52 GMT  
 how to remove trailing blanks of a string?
I would do what Paul has suggested here, except that I'd run the loop backwards--start from the
end and keep moving toward the front of the string looking for something that isn't a space.  The
first non-space you find is the length of your string.

do i=len(test_string),1,-1
  length = i
  if (test_string(i:i) .ne. ' ') goto 17
end do

17    continue
...

I don't remember whether exit is a valid statement for leaving a do loop in f77.  If it is, you
can use exit rather than a goto to get out of the loop.

-Doug Magnoli
[Delete the two and the three for email.]

Quote:


> > hi, there,

> > i am using g77.

> > i am trying to remove some trailing blanks of a string. is there some
> > built in functions that can be used?

> > for example,

> > character*15 test_string
> > test_string = 'temp'
> > open(10,file=test_string//'.dat',status='new')

> > i will end up with temp       .dat

> > one way i can think of is that when declare test_string, do not setup
> > the length of string. can fortran automaticlly set up a string w/o a
> > length?

> > or use a function to remove the trailing blanks.

> > could someone tell me how to do?

> Hmm, apart from checking if len_trim() (or trim() for that matter) is accepted in g77, you
> might try:

>   character*15 test_string
>   integer i, length

>   test_string = 'temp'

>   length = 0
>   DO i = 1, LEN( test_string )
>     IF ( test_string(i:i) .NE. ' ' ) THEN
>       length = length + 1
>     END IF
>   END DO

>   OPEN(10,file=test_string(1:length)//'.dat',status='new')

> It is a bit longwinded and it does rely on the fact that everything else in the string will be
> a space (which may or may not be the correct behaviour - I don't know what the standard
> dictates in that respect). I would suggest sticking this in it's own function and use it
> similarly to the f90 LEN_TRIM().

> Also, in the past I have found that passing strings into routines, sticking stuff in them and
> then passing them back sometimes filled the unsed/unassigned bits of the string with random
> {*filter*}so I got into the habit of "zeroing out" strings with spaces before I assigned them.
> Probably not an issue anymore, but you never know.

> Alternatively, if you can get your hands on a f90/95 compiler you can do what David Frank
> suggested with the TRIM() intrinsic in another post. Also, I _believe_ the f90 standard
> dictates that the unsed/unassigned parts of string be filled with blanks (based on reading
> "Fortran 90/95 Explained"
> by M. Metcalf and John Reid).

> cheers,

> paulv

> --
> Paul van Delst             Religious and cultural

> Ph: (301)763-8000 x7274    fantasy
> Fax:(301)763-8545                   V.S.Naipaul



Mon, 12 Jul 2004 23:34:35 GMT  
 how to remove trailing blanks of a string?

Quote:
>> i am trying to remove some trailing blanks of a string. is there some
>> built in functions that can be used? ....
>Hmm, apart from checking if len_trim() (or trim() for that matter) is
>accepted in g77, you might try: ....

RUN IT BACKWARDS:
   DO Length = LEN(test_string), 1, -1
     IF (test_string(i:i) .NE. ' ') EXIT (or otherwise break the loop)
   END DO
   OPEN(10,file=test_string(1:length)//'.dat',status='new')

--Myron.
--
Five boxes preserve our freedoms:  soap, ballot, witness, jury, and cartridge
PhD EE (retired).   "Barbershop" tenor.   CDL(PTX).   W0PBV.   (785) 539-4448
NRA Life Member and Certified Instructor (Home Firearm Safety, Rifle, Pistol)



Mon, 12 Jul 2004 23:24:33 GMT  
 how to remove trailing blanks of a string?

Quote:


> > hi, there,

> > i am using g77.

> > i am trying to remove some trailing blanks of a string. is there some
> > built in functions that can be used?

> > for example,

> > character*15 test_string
> > test_string = 'temp'
> > open(10,file=test_string//'.dat',status='new')

> > i will end up with temp       .dat

> > one way i can think of is that when declare test_string, do not setup
> > the length of string. can fortran automaticlly set up a string w/o a
> > length?

> > or use a function to remove the trailing blanks.

> > could someone tell me how to do?

> Hmm, apart from checking if len_trim() (or trim() for that matter) is accepted in g77, you
> might try:

>   character*15 test_string
>   integer i, length

>   test_string = 'temp'

>   length = 0
>   DO i = 1, LEN( test_string )
>     IF ( test_string(i:i) .NE. ' ' ) THEN
>       length = length + 1
>     END IF
>   END DO

>   OPEN(10,file=test_string(1:length)//'.dat',status='new')

> It is a bit longwinded

[snip]

you can also use the INDEX function

  length = index(test_string,' ') - 1
   OPEN(10,file=test_string(1:length)//'.dat',status='new')

INDEX returns the substring index value of the first instance of ' '
and is easier for me to remember.

He could also do

OPEN(10,file=test_string(1:index(test_string,' ') -
1)//'.dat',status='new')

but I think that's hard to read.

Dick Hendrickson



Tue, 13 Jul 2004 01:08:24 GMT  
 how to remove trailing blanks of a string?

the frequently asked question...

Quote:
> i am using g77.

> i am trying to remove some trailing blanks of a string....
> is there some built in functions that can be used?

In f90, you'd just use the trim intrinsic.  But that isn't
available in f77.  In f77 you'll need to explicitly use
a substring.  That is, instead of

Quote:
> open(10,file=test_string//'.dat',status='new')

write

Quote:
> open(10,file=test_string(:iend)//'.dat',status='new')

where you have computed iend to be the index of the last non-blank
character in string.  That computation is left as a relatively easy
excercise - in f90 you'd use len_trim, but if you had f90, you'd
just use trim in the first place.  One caveat - f77 substrings
can't be zero-length, so if test_string can ever be completely
blank, you need to specila case that.  (Again, f90 is nicer here.)

Quote:
> one way i can think of is that when declare test_string, do not setup
> the length of string. can fortran automaticlly set up a string w/o a
> length?

No.  You can't do anything like that.  Trying will probably just cause
you frustration.  If you don't specify the length of a string, then
the length defaults to 1 - not to arbitrary.  You can declare a string
dummy argument that assumes its length from the actual argument, but
that won't really do you much good for this purpose.

--
Richard Maine                       |  Good judgment comes from experience;
email: my last name at host.domain  |  experience comes from bad judgment.
host: altair, domain: dfrc.nasa.gov |        -- Mark Twain



Tue, 13 Jul 2004 01:09:43 GMT  
 how to remove trailing blanks of a string?

Quote:
> Also, I _believe_ the f90 standard dictates that the
> unsed/unassigned parts of string be filled with blanks (based on
> reading "Fortran 90/95 Explained" by M. Metcalf and John Reid).

There is some truth in that statement, but you've phrased it a little
too broadly.  What you are describing only applies to a few situations.
They are the most common ways to get a string defined, but they
aren't the only ones.  The two situations that I can off-hand think of
where this applies are

1. Assignment to the whole string, as in

      string = .....

   In this case, the entire string is defined, using blank-fill if needed.

2. Input to the whole string, as in

      read (.....) string

   Again, the whole string will get defined one way or another (unless the
   read fails, for example, with an end-of-file or some such).

The "whole string" bit is the important element in both the above situations.
It is possible to define substrings, as in

   string(i:j) = ....

In this case, yes, you'll still get blank-filling if needed, but *ONLY*
in the substring from i to j.  The rest of the string is unaffected.  If
it was undefined before, it still is.

So yes, strings can have undefined garbage in them.  It is a property
of character assignment and input that does the blank filling.  There is
not a general property of strings that undefined parts are blank.  Undefined
parts are...undefined.  But assignment to a whole string doesn't leave any
undefined parts - it defines the whole string, with blanks if needed.

--
Richard Maine                       |  Good judgment comes from experience;
email: my last name at host.domain  |  experience comes from bad judgment.
host: altair, domain: dfrc.nasa.gov |        -- Mark Twain



Tue, 13 Jul 2004 01:21:00 GMT  
 how to remove trailing blanks of a string?

Quote:

> >> i am trying to remove some trailing blanks of a string. is there some
> >> built in functions that can be used? ....


> >Hmm, apart from checking if len_trim() (or trim() for that matter) is
> >accepted in g77, you might try: ....

> RUN IT BACKWARDS:
>    DO Length = LEN(test_string), 1, -1
>      IF (test_string(i:i) .NE. ' ') EXIT (or otherwise break the loop)
>    END DO
>    OPEN(10,file=test_string(1:length)//'.dat',status='new')

Oops. Yes - run it backwards. A much smarter method.

cheers,

paulv

--
Paul van Delst             Religious and cultural

Ph: (301)763-8000 x7274    fantasy
Fax:(301)763-8545                   V.S.Naipaul



Tue, 13 Jul 2004 01:41:59 GMT  
 how to remove trailing blanks of a string?
Note that G77 is being used and G77 does provide the LEN_TRIM() function.
Therefore, the following should work:

      character *80 string
      ...
      string = 'TEST'
      ...
      open (unit=1,file=(string(1:len_trim(string))//'.DAT'),...)
      ...
      read (1, ...) list

I tried this out with MinGW G77 and it worked fine.



Tue, 13 Jul 2004 01:44:37 GMT  
 how to remove trailing blanks of a string?

Quote:

> Note that G77 is being used and G77 does provide the LEN_TRIM() function.
> Therefore, the following should work:
...
>       open (unit=1,file=(string(1:len_trim(string))//'.DAT'),...)

Do be aware that, as I alluded to in a separate post, this is illegal
f77 if string is completely blank.  That's because len_trin would
then return 0 (it had better if it is to be f90-compatable), and
the substring

  string(1:0)

is illegal in f77.  Of course, it is possible that g77 also supports
zero-length substrings as another extension.  Just be aware that this
needs *TWO* separate extensions to work.  The len_trim is the most
trivial of those.  (After all, it is but a few minutes of work to
write a user-written version of len_trim for the scalar case).  The
handling of zero-length substrings has far more implications.  (Then
assumed-length dummy strings might have zero length, which better be
handled correctly, etc).  Perhaps a blank string isn't "supposed" to
be a valid option, but if there is a way it could possibly happen, and
the compiler doesn't handle zero-length strings as an extension, then
code like the above is a bug waiting to surface.

In my f77 days, I used to have my own function, a lot like len_trim,
but special-casing to never return the value 0.  If the input string
was all blank, I returned the value 1.  That might end up giving me
an undesired blank, but at least it avoided strange run-time aborts.

Really was (still is for some people) a nuisance that f77 didn't handle
zero-length strings naturally.  I regard it as very simillar to the
way that f66 didn't handle zero-trip DO loops naturally (it said
they were illegal).  F77 fixed DO loops so that you didn't need to
special-case any one that might have a zero trip count.  F90 fixed a
very simillar issue with substrings.

--
Richard Maine                       |  Good judgment comes from experience;
email: my last name at host.domain  |  experience comes from bad judgment.
host: altair, domain: dfrc.nasa.gov |        -- Mark Twain



Tue, 13 Jul 2004 02:35:11 GMT  
 how to remove trailing blanks of a string?


Quote:
>you can also use the INDEX function

>  length = index(test_string,' ') - 1
>   OPEN(10,file=test_string(1:length)//'.dat',status='new')

But if
(1) test_string contains no blanks at all, then length = -1, or
(2) test_string begins with a blank, then length = 0.
In either case test_string(1:length) will fail.

John Harper, School of Mathematical and Computing Sciences,
Victoria University, PO Box 600, Wellington, New Zealand



Tue, 13 Jul 2004 04:47:19 GMT  
 how to remove trailing blanks of a string?
hi, all,

i really appreciate all your helps.

in summary there are about 3 type of ways to do it. (1) use trim().
but it is a function in f90, maybe not all f77 compilers can recognize
it.  (2) write a little program to search for ' '. it is ok to use.
(3) use index to locate ' '. i think it is easier than writting a
little program. in fact, today i also came up with this idea myself.

by the way, if no space can be found by INDEX, it will return 0. so
below is my program:

i=index(test_string,' ')

if(i.ne.0) then
    open(10,file=test_string(1:i-1)//'.dat')
else
    open(10,file=test_string//'.dat')
endif

it works very well.

again, thank you very much.

richard

ps, of course, this program assumes that there is no space at the
beginning of test_string



Tue, 13 Jul 2004 12:13:56 GMT  
 how to remove trailing blanks of a string?

Quote:

> hi, all,

> i really appreciate all your helps.

> in summary there are about 3 type of ways to do it. (1) use trim().
> but it is a function in f90, maybe not all f77 compilers can recognize
> it.  (2) write a little program to search for ' '. it is ok to use.
> (3) use index to locate ' '. i think it is easier than writting a
> little program. in fact, today i also came up with this idea myself.

> by the way, if no space can be found by INDEX, it will return 0. so
> below is my program:

> i=index(test_string,' ')

> if(i.ne.0) then
>     open(10,file=test_string(1:i-1)//'.dat')
> else
>     open(10,file=test_string//'.dat')
> endif

> it works very well.

I would prefer:

 i=index(test_string,' ')

 if ( i .gt. 1 ) then
   filename = test_string(1:i-1)//'.dat'
 else
   filename = 'default_filename.dat'
 end if

 open( 10, file = filename )

I don't like having two OPEN statements when one will do. Makes it easier modifying the code if
you ever want to add additional keywords to the OPEN - only have to do it in one spot.

It also avoids the filename ".dat" if there *are* left justified blanks (well, you never
know....).

cheers,

paulv

--
Paul van Delst             Religious and cultural

Ph: (301)763-8000 x7274    fantasy
Fax:(301)763-8545                   V.S.Naipaul



Tue, 13 Jul 2004 23:09:55 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Removing trailing blanks

2. removing trailing spaces from string variables

3. remove blank lines before lines beginning with string

4. J for delete trailing blanks[Q]

5. Stripping leading and trailing blanks - How?

6. File Conversion: UNIX.txt to DOS.txt and trim trailing blanks

7. Way to find the trailing blanks

8. trailing blanks format?

9. Modifying existing script to remove trailing newlines

10. Remove trailing newlines from file

11. Removing trailing spaces from a field using awk

12. Elisp--Remove a trailing NULL char?

 

 
Powered by phpBB® Forum Software