Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
56
Task/Align-columns/Perl/align-columns-1.pl
Normal file
56
Task/Align-columns/Perl/align-columns-1.pl
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#/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 "last argument must be one of center, left or right!\n" unless
|
||||
$ARGV[ 1 ] =~ /center|left|right/ ;
|
||||
sub printLines( $$$ ) ;
|
||||
open INFILE , "<" , "$ARGV[ 0 ]" or die "Can't open $ARGV[ 0 ]!\n" ;
|
||||
my @lines = <INFILE> ;
|
||||
close INFILE ;
|
||||
chomp @lines ;
|
||||
my @fieldwidths = map length, split /\$/ , $lines[ 0 ] ;
|
||||
foreach my $i ( 1..$#lines ) {
|
||||
my @words = split /\$/ , $lines[ $i ] ;
|
||||
foreach my $j ( 0..$#words ) {
|
||||
if ( $j <= $#fieldwidths ) {
|
||||
if ( length $words[ $j ] > $fieldwidths[ $j ] ) {
|
||||
$fieldwidths[ $j ] = length $words[ $j ] ;
|
||||
}
|
||||
}
|
||||
else {
|
||||
push @fieldwidths, length $words[ $j ] ;
|
||||
}
|
||||
}
|
||||
}
|
||||
printLine( $_ , $ARGV[ 1 ] , \@fieldwidths ) foreach @lines ;
|
||||
################################################################## ####
|
||||
sub printLine {
|
||||
my $line = shift ;
|
||||
my $orientation = shift ;
|
||||
my $widthref = shift ;
|
||||
my @words = split /\$/, $line ;
|
||||
foreach my $k ( 0..$#words ) {
|
||||
my $printwidth = $widthref->[ $k ] + 1 ;
|
||||
if ( $orientation eq 'center' ) {
|
||||
$printwidth++ ;
|
||||
}
|
||||
if ( $orientation eq 'left' ) {
|
||||
print $words[ $k ] ;
|
||||
print " " x ( $printwidth - length $words[ $k ] ) ;
|
||||
}
|
||||
elsif ( $orientation eq 'right' ) {
|
||||
print " " x ( $printwidth - length $words[ $k ] ) ;
|
||||
print $words[ $k ] ;
|
||||
}
|
||||
elsif ( $orientation eq 'center' ) {
|
||||
my $left = int( ( $printwidth - length $words[ $k ] ) / 2 ) ;
|
||||
my $right = $printwidth - length( $words[ $k ] ) - $left ;
|
||||
print " " x $left ;
|
||||
print $words[ $k ] ;
|
||||
print " " x $right ;
|
||||
}
|
||||
}
|
||||
print "\n" ;
|
||||
}
|
||||
24
Task/Align-columns/Perl/align-columns-2.pl
Normal file
24
Task/Align-columns/Perl/align-columns-2.pl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use List::Util qw(max);
|
||||
|
||||
sub columns {
|
||||
my @lines = map [split /\$/] => split /\n/ => shift;
|
||||
my $pos = {qw/left 0 center 1 right 2/}->{+shift};
|
||||
for my $col (0 .. max map {$#$_} @lines) {
|
||||
my $max = max my @widths = map {length $_->[$col]} @lines;
|
||||
for my $row (0 .. $#lines) {
|
||||
my @pad = map {' ' x $_, ' ' x ($_ + 0.5)} ($max - $widths[$row]) / 2;
|
||||
for ($lines[$row][$col])
|
||||
{$_ = join '' => @pad[0 .. $pos-1], $_, @pad[$pos .. $#pad]}
|
||||
}
|
||||
}
|
||||
join '' => map {"@$_\n"} @lines
|
||||
}
|
||||
|
||||
print columns <<'END', $_ for qw(left right center);
|
||||
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
|
||||
are$delineated$by$a$single$'dollar'$character,$write$a$program
|
||||
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
|
||||
column$are$separated$by$at$least$one$space.
|
||||
Further,$allow$for$each$word$in$a$column$to$be$either$left$
|
||||
justified,$right$justified,$or$center$justified$within$its$column.
|
||||
END
|
||||
Loading…
Add table
Add a link
Reference in a new issue