Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29235 Discussions

Help with trimming character variable

remkoduursma
Beginner
513 Views
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





0 Kudos
1 Solution
bmchenry
New Contributor II
513 Views
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

View solution in original post

0 Kudos
2 Replies
Steven_L_Intel1
Employee
513 Views
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.
0 Kudos
bmchenry
New Contributor II
514 Views
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
0 Kudos
Reply