- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry for the noob question - but I have trouble using TRIM on a character string to get rid of trailing blanks.
PTITLES is an array, defined as
CHARACTER PTITLES(10)*(*)
I read it from a file like this, in a loop:
READ (UPHY, *) PTITLES(I)
There are plenty of trailing spaces,
so I do:
PTITLES(I) = TRIM(PTITLES(I))
The result is not modified at all from the original...
any help?
thanks,
Remko
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If output format/spacing is all you are worried about, then a way to handle trailing spaces is to use the LEN_TRIM function:
namelength(i) = Len_Trim(filename(i))
Then to write simply use:
write(*,*) Filename(i)(:namelength(i))
clips the spaces on output
namelength(i) = Len_Trim(filename(i))
Then to write simply use:
write(*,*) Filename(i)(:namelength(i))
clips the spaces on output
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When you assign the trimmed value to PITTLES(I), it gets blank padded again to its origibal length. Typically you do the TRIM at the point where you are going to display or use the value, for example:
WRITE (*,*) "xxx" // TRIM(PITTLES(I)) // "yyy"
Version 11.1 supports the Fortran 2003 feature of assumed-length allocatable character variables so you could have something like this:
CHARACTER(:), ALLOCATABLE :: VARYING_STRING
VARYING_STRING = 'ABCD' ! Length 4
VARYING_STRING = '12345' ! Length 5
In the current update, you need to compile with /assume:realloc_lhs to get this to work, but that requirement will disappear in the next (I think) update.
Note that you can't just slap this in to your existing code as if you're passing these as arguments then explicit interfaces are required.
WRITE (*,*) "xxx" // TRIM(PITTLES(I)) // "yyy"
Version 11.1 supports the Fortran 2003 feature of assumed-length allocatable character variables so you could have something like this:
CHARACTER(:), ALLOCATABLE :: VARYING_STRING
VARYING_STRING = 'ABCD' ! Length 4
VARYING_STRING = '12345' ! Length 5
In the current update, you need to compile with /assume:realloc_lhs to get this to work, but that requirement will disappear in the next (I think) update.
Note that you can't just slap this in to your existing code as if you're passing these as arguments then explicit interfaces are required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If output format/spacing is all you are worried about, then a way to handle trailing spaces is to use the LEN_TRIM function:
namelength(i) = Len_Trim(filename(i))
Then to write simply use:
write(*,*) Filename(i)(:namelength(i))
clips the spaces on output
namelength(i) = Len_Trim(filename(i))
Then to write simply use:
write(*,*) Filename(i)(:namelength(i))
clips the spaces on output

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page