For Programmers: Free Programming Magazines  


Home > Archive > Fortran > December 2006 > Write A, left justified, howto?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Write A, left justified, howto?
Jeremy

2006-12-28, 4:03 am

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
Arjen Markus

2006-12-28, 4:03 am


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

Dick Russell

2006-12-28, 8:03 am


Arjen Markus wrote:
> Jeremy schreef:
>
....
[color=darkred]
> The A edit descriptor is explicitly designed to do it that way, but how
> about
> a small helper function?
>
> program padding


Assuming your variables are defined with lengths that correspond to the
lengths of the printed fields, and all you want to do is print each
left justified, then ADJUSTL by itself will do; it removes leading
blanks and pads on the right with blanks.

Dick Russell

2006-12-28, 8:03 am


Dick Russell wrote:[color=darkred]

I replied too quickly. I notice that you are printing literals, which
you want padded. One other solution would be to leave the literals,
unpadded, as is in the WRITE statement and change the FORMAT string to
use just A for the literal followed by a Tn to tab to the column for
the next literal.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com