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

Concatenate a slash

Lee_B_3
Novice
704 Views

Greetings,

   I have a function that scans a character array looking for a set of defined delimiters (space, coma, slash, etc.) and returns the "field", a copy of the original array without the "field" and delimiter, and the delimiter.  In some cases I then want to concatenate the delimiter to the "field" and continue scanning.

   For example:  array = "This is a test/and an example, and another thing"

   Let's say I want to retain all the text up to the comma, so successive calls return:

             "This", "is", "a", all with delimiter space.  For the output these, with their delimiters are concatenated to "This is a "

             "test" with delimiter slash.  When I try to concatenate "test" and "/", I then get "This is a test ".

   I suspect the compiler gets confused here and is treating the single slash as a meta-character or something. 

   I just thought I can test for the delimiter being a slash and concatenate CHAR(47)...

   If I didn't just answer my own question, or if I did, please let me know.  lol

Take care,

Lee B.

0 Kudos
2 Replies
Steve_Lionel
Honored Contributor III
704 Views

You haven't shown any code. But a common problem is users not understanding that Fortran character variables are fixed-size, so if you do something like this:

character(20) :: string
string = 'ABCDEF'
string = string // '/'
print *, string
end

You won't see the slash. Why? Because the result of the concatenation is 21 characters and the 21st gets dropped when assigned to the 20-character variable.

Might this be your issue? If so, TRIM is your friend.

Edit: Deferred-length allocatable character variables are different, but you're probably not using them here.

0 Kudos
gib
New Contributor II
704 Views

In case what Steve wrote isn't crystal clear:

In his example, 

string = 'ABCDEF'

actuallly means

string = 'ABCDEF              '

i.e. there are 14 spaces after ABCDEF, since string is 20 characters long, and a character variable is always padded with spaces to its declared length.  To append a '/' after 'F', it is necessary to append the extra character after trimming off the added spaces:

string = trim(string) // '/'

which gives

string = 'ABCDEF/             '

 

 

0 Kudos
Reply