Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Output TRIMmed list of strings

Status
Not open for further replies.

mojomother

Programmer
Apr 20, 2010
9
DE
In my code, I declare a large list of fixed-length character strings:

Code:
CHARACTER * 256 :: string_list(256)

The ancient problem is that if I was to output such a string, it is always filled with spaces, no matter what its actual contents are:

Code:
string_list(1) = 'test'
WRITE(*,'(A)') string_list(1)


OUTPUT: "test                                                                                                                                                                                           "

When writing a single string, this is easily worked around by using TRIM:

Code:
string_list(1) = 'test'
WRITE(*,'(A)') TRIM(string_list(1))


OUTPUT: "test"


However, I want to output the entire list, i.e.

Code:
WRITE(*,'(256A)') string_list

In this case, TRIM can not so easily be applied since it doesn't work on an array. How can I output this string array with each individual string being written with its actual - rather than fixed - length?

Thank you very much for any help!
Matt
 
How about
Code:
write(*, '(256A)') (trim(string_list(ii)), ii = 1, 256)
 
Works perfectly! Thank you so much!

I didn't know about this kind of constructs at all (iterating without DO blocks). Can you tell me what they are called?
 
This is an implied do loop. Normally used with read and write /print statements. You can also use them for array initialization (google array initialization)
Code:
! a(1) = 21, a(2) = 72 ...
data (a(j),j=1,4)/21.,72.,23.,34./
! All elements to 10
real, dimension(4):: b = (/(10, i = 1, 4)/)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top