Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenting Strings Help 1

Status
Not open for further replies.

mjs84

Aerospace
Aug 20, 2003
27
I need to build a string based on some input data. The data is coming from user input from a motif application so I don't know the length of any piece of data.
Here is my code:

character*50 str0,s0
character*50 str1,s1
character*50 str2,s2
character*50 str3,s3
character*10 str4,s4
character*10 str5,s5
character*70 elemID
integer*4 lenchr

c *** note: s0 thru s5 are variable from motif applic.***

str0 = s0(1:lentrim(s0))
str1 = s1(1:lentrim(s1))
.
.
.
str6 = s6(1:lentrim(s6))

elemID = str0 // ',' // str1 // ',' // str2 // ',' //
& str3 // ',' // str4 // ',' // str5

open(1,"filename", status="UNKNOWN")
write(1,'(a,2x,i4)') elemID, lenchr

c *** end of code ***

When I print out the values, I get the correct values for all the separate variables, ie...
str0 = base
str1 = A
str2 = B
str3 = C
str4 = D
str5 = E

When I print out 'eleID' and 'lenchr', I expect this:
'base,A,B,C,D,E 14'
... but what i get is:
'base 4'

I am assuming this has something to do with the string declarations, but I'm not sure. I know that when all the strings are added up, they will be less than 70 characters, but I don't know the length of any of the particular fields.

Any help would be greatly appreciated.

Regards,
mjs84



 
Replies continue below

Recommended for you

Consider what you are concatenating. STR0 through STR3 are each length 50 characters. Concatenating just the first two (with that comma in between) overflows "elemID". You ought to do your LEN_TRIMming and concatenating all together:

elemID=len_trim(s0)//','//len_trim(s1)//','//....etc.

That will eliminate all those trailing blanks, as well as save the auxilliary storage variables, which, as you coded them, wind up being exact copies (trim the blanks, then pad with blanks again to fill the variable to its defined length).
 
Whew! I got back here before someone else pointed out the flaw in my advice! When I awoke this morning, it dawned on me that in my suggestion to combine the trimming and concatenation, I mistakenly used LEN_TRIM (which returns the length with blanks trimmed), when I should have used just TRIM (which returns the trimmed string itself instead of the length). The revised line would be then this:

elemID=trim(s0)//','//trim(s1)//','//....etc.

What an embarassment! But I hope I have redeemed myself. HTH.
 
DickRussell,

Thanks for your help.

mjs84
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor