aboutsummaryrefslogtreecommitdiff
path: root/challenge-008/andrezgz/perl5/ch-2.pl
blob: 5962e8401aa64d62c1bbe700fc7808ccb237155f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl

# https://perlweeklychallenge.org/blog/perl-weekly-challenge-008/
# Challenge #2
# Write a function, 'center', whose argument is a list of strings, which will be lines of text.
# The function should insert spaces at the beginning of the lines of text so that if they were printed,
# the text would be centered, and return the modified lines.

use strict;
use warnings;

print join $/, center("This", "is", "a test of the", "center function");

sub center {
    my @lines = @_;
    my $max = 0;
    map { $max = length($_) if (length($_) > $max) } @lines;
    return map { ' 'x(($max-length($_))/2).$_ } @lines
}