Align columns: Difference between revisions

Line 988:
 
=={{header|Perl}}==
<lang Perl>#/usr/bin/perl -w
use strict ;
#/usr/bin/perl -w
 
use strict ;
die "Call : perl columnaligner.pl <inputfile> <printorientation>!\n" unless
@ARGV == 2 ; #$ARGV[ 0 ] contains example file , $ARGV[1] any of 'left' , 'right' or 'center'
die "Call : perl columnaligner.pl <inputfile> <printorientation>!\n" unless
die "last argument must be one of center, left or right!\n" unless
@ARGV == 2 ; #$ARGV[ 0 ] contains example file , $ARGV[1] any of 'left' , 'right' or 'center'
die$ARGV[ "last1 argument] must be one of=~ /center, |left or |right!\n"/ unless;
sub printLines( $$$ ) ;
$ARGV[ 1 ] =~ /center|left|right/ ;
open ( INFILE , "<" , "$ARGV[ 0 ]" ) or die "Can't open $ARGV[ 0 ]!\n" ;
sub printLines( $$$ ) ;
my @lines = <INFILE> ;
open ( INFILE , "<" , "$ARGV[ 0 ]" ) or die "Can't open $ARGV[ 0 ]!\n" ;
close my @lines = <INFILE> ;
chomp @lines ;
close ( INFILE ) ;
my @fieldwidths = map {length, chompsplit /\$_/ }, @$lines[ 0 ] ;
foreach my @fieldwidths$i =( (1..$#lines ) ;{
map { pushmy @fieldwidthswords , length( $_ ) }= split ( /\$/ , $lines[ 0$i ] ) ;
foreach my $ij ( 10..$#lineswords ) {
my @wordsif = split( /\$/j ,<= $lines[ $i ]#fieldwidths ) ;{
foreach my $j if ( 0..length $#words[ $j ] > $fieldwidths[ $j ] ) {
if ( $fieldwidths[ $j <] = length $#fieldwidthswords[ $j )] {;
}
if ( length( $words[ $j ] ) > $fieldwidths[ $j ] ) {
}
$fieldwidths[ $j ] = length( $words[ $j ] ) ;
else }{
push if (@fieldwidths, length( $words[ $j ] ) > $fieldwidths[ $j ] ) {;
}
else {}
}
push @fieldwidths, length( $words[ $j ] ) ;
}
}
map { printLine( $_ , $ARGV[ 1 ] , \@fieldwidths ) }foreach @lines ;
}
################################################################## ####
}
sub printLine {
map { printLine( $_ , $ARGV[ 1 ] , \@fieldwidths ) } @lines ;
my $line = shift ;
################################################################## ####
if (my $orientation eq 'center'= )shift {;
sub printLine {
my $linewidthref = shift ;
my $orientation@words = shiftsplit /\$/, $line ;
foreach my $widthrefk =( 0..$#words shift) ;{
my @words$printwidth = split( /\$/,widthref->[ $linek ] + )1 ;
foreach my $kif ( 0..$#wordsorientation eq 'center' ) {
my $printwidth = $widthref->[ $k ] + 1+ ;
}
if ( $orientation eq 'center' ) {
if ( $orientation eq 'left' $printwidth++) ;{
} print $words[ $k ] ;
if print " " x ( $orientationprintwidth eq- length $words[ $k 'left'] ) {;
}
print $words[ $k ] ;
ifelsif ( $orientation eq 'centerright' ) {
for ( my $i = 0 ; $i < $printwidth - length( $words[ $k ] ) ; $i++ ) {
print " " x ( print$printwidth "- "length $words[ $k ] ) ;
print $words[ }$k ] ;
}
ifelsif ( $orientation eq 'rightcenter' ) {
for ( my $ileft = 0int( ; $i <( $printwidth - length( $words[ $k ] ) ) ;/ $i++2 ) {;
my $right = $printwidth - length( $words[ $k ] ) - $left print " " ;
print " }" x $left ;
print $words[ $k ] ;
} print " " x $right ;
}
if ( $orientation eq 'center' ) {
}
my $left = int( ( $printwidth - length( $words[ $k ] ) ) / 2 ) ;
print "\n" ;
my $right = $printwidth - length( $words[ $k ] ) - $left ;
}</lang>
if ( $left == 1 ) {
print " " ;
}
if ( $left > 1 ) {
for ( my $i = 0 ; $i < $left ; $i++ ) {
print " " ;
}
}
print $words[ $k ] ;
if ( $right == 1 ) {
print " " ;
}
if ( $right > 1 ) {
for ( my $i = 0 ; $i < $right ; $i++ ) {
print " " ;
}
}
}
}
print "\n" ;
}
</lang>
a shorter solution
<lang perl>
Anonymous user