[ale] center justification in column using awk or perl

Danny Cox danscox at mindspring.com
Tue Jun 18 13:16:25 EDT 2002


Dow,

On Tue, 2002-06-18 at 13:04, Dow Hurst wrote:
> Here is the code I have to work 
> with in Perl currently that needs to be transformed from a printf to a 
> format/write.
> 
> for ( $i = 0; $i <= $#AtomType; $i++ ) {
> 
>     printf CLSTout "%4d %5d %d %5d %d %5d %d %5d %d %5d %d %5d %d %11.6f 
> %11.6f %11.6f %7s%4d%9.5f%9.5f %4s %4s\n", $AtomType[$i], 
> $$connect[$i][0], $$bonding[$i][0],$$connect[$i][1], $$bonding[$i][1], 
> $$connect[$i][2], $$bonding[$i][2], $Connect7[$i], $Connect8[$i], 
> $Connect9[$i], $Connect10[$i], $Connect11[$i], $Connect12[$i], 
> $Xcoord[$i], $Ycoord[$i], $Zcoord[$i], $Thing[$i], $AtomColor[$i], 
> $Charge1[$i], $Charge2[$i], $ResName[$i], $AtomName[$i];
> 
> 
> All I want to do is center justify the last column in the printf 
> statement.   Thanks for the help!  I am working on the format statement 
> but don't really know how to set it up.  I know I should use a "@||||" 
> for the last column in the format statement but then I don't know how to 
> setup up the decimal places in the columns that need them.

	You could go two ways (prob. more).

	1) use a %-10s specifier to get the width you need, and use sprintf on
$AtomName[$i] to get the number of leading spaces you want.

	sprintf ($tmp, "%*s", (10 - strlen ($AtomName[$i]))/2, $AtomName[$i]);

	2) add another %*s in the printf, just before the last format spec, and
add a similiar expression just before the last arg:

		$ResName[$i], (10 - strlen ($AtomName[$i]))/2, $AtomName[$i];

	You can also make the "10" a integer, if you wish.  The second way is a
little easier, and perhaps clearer.

-- 
kernel, n.: A part of an operating system that preserves the
medieval traditions of sorcery and black art.

Danny


---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list