blob: 89bd6dab3a1ebd15383446d35f8ed473d766c8eb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
use strict;
use warnings;
use Const::Fast;
const my $PREFIX => ' ';
const my $SPACE => ' ';
MAIN:
{
my @input = ('This', 'is', 'a test of the', 'center function');
my @output = center(@input);
print "\n";
print $PREFIX . $_ . "\n" for @output;
}
sub center
{
my @lines = @_;
my $max = 0;
my @lengths;
for my $line (@lines)
{
my $len = length $line;
push @lengths, $len;
$max = $len if $len > $max;
}
my @centered;
for my $i (0 .. $#lines)
{
my $spaces = int(($max - $lengths[$i]) / 2);
push @centered, ($SPACE x $spaces) . $lines[$i];
}
return @centered;
}
|