Re: Write A, left justified, howto?




Jeremy schreef:

How can I make the following print ID, Code and Name left justified not
right? I found that if I pad ID, Code and Name to be longer than the
desired output then it becomes left justified.

write(*, '(A4,1X,A10,1X,A10)') 'ID', 'Code', 'Name'

prints:

ID Code Name
---- ---------- ----------

(-'s added for clarity)

write(*, '(A4,1X,A10,1X,A10)') 'ID ', 'Code ', &
'Name '

will print:

ID Code Name
---- ---------- ----------

which is what I want but I'm sure there has to be a better way of doing
it.

Thanks for any input,

Jeremy

The A edit descriptor is explicitly designed to do it that way, but how
about
a small helper function?

program padding

write(*, '(A4,1X,A10,1X,A10)') 'ID', 'Code', 'Name'
write(*, '(A,1X,A,1X,A)') adj('ID',4), adj('Code',10),
adj('Name',10)

contains
function adj(string,length) result(r)
character(len=*) :: string
integer :: length
character(len=length) :: r

r = adjustl(string)
end function adj
end program

This little program uses an (almost) trivial function adj() to do the
job of creating a string padded with spaces of the right length.

No need to use the width field with the A descriptor then either.

Regards,

Arjen

.


Quantcast