aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-019/mark-anderson/perl5/ch-2.pl22
1 files changed, 22 insertions, 0 deletions
diff --git a/challenge-019/mark-anderson/perl5/ch-2.pl b/challenge-019/mark-anderson/perl5/ch-2.pl
new file mode 100644
index 0000000000..4fdb6a87ff
--- /dev/null
+++ b/challenge-019/mark-anderson/perl5/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+
+use Modern::Perl '2018';
+
+my $line_break = $/;
+my $space_width = 1;
+my $line_width = 72;
+my @text = split /\s+/, join '', <>;
+my $space_left = $line_width;
+
+foreach my $word (@text) {
+ if((length $word) + $space_width > $space_left) {
+ $word = $line_break . $word;
+ $space_left = $line_width - (length $word);
+ }
+
+ else {
+ $space_left -= (length $word) + $space_width;
+ }
+}
+
+say "@text";