Sunday, January 25, 2015

Left or Right Justify Perl

#!/usr/bin/perl
my $file = 'input';
my @data;
open( FILE, $file ) or die "Can't open file '$file': $!";
while( ) {
chomp;
my @row = split;
push @data, \@row;
}
close( FILE );
#Print output
for my $i (0..$#data)
{
printf  "%5d",$data[$i][0];  #allocate 5 digits integer format
printf  "%10d",$data[$i][1]; #allocate 8 digits integer format, since the maxium digit in this col is 8 the result will provide 2 digits gaps
printf  "%1d",$data[$i][2];  #allocate 1 digit integer, no space with the previous col
printf  "%-6s",$data[$i][3];  #allocate 6 digit string (LEFT JUSTIFY)
printf  "%9.2f",$data[$i][4];#allocate 9 digits float with 2 decimal precission
print "\n";
}

No comments: