aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-07-30 19:05:44 +0100
committerGitHub <noreply@github.com>2019-07-30 19:05:44 +0100
commit74ce2ad7e28ee03bdb092cecbafbcef9b6c9d1ac (patch)
tree60a9d8e3990b77779c96f00d7c32ee5af46423f9
parentfa6d2c6f00f7a9067c5366cbeabfba77bb727b61 (diff)
parent8410093dc12277eb89090a90db4dc33ab616d88a (diff)
downloadperlweeklychallenge-club-74ce2ad7e28ee03bdb092cecbafbcef9b6c9d1ac.tar.gz
perlweeklychallenge-club-74ce2ad7e28ee03bdb092cecbafbcef9b6c9d1ac.tar.bz2
perlweeklychallenge-club-74ce2ad7e28ee03bdb092cecbafbcef9b6c9d1ac.zip
Merge pull request #451 from davorg/master
Added solution to challenge 2
-rw-r--r--challenge-019/dave-cross/perl5/ch-2.pl34
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-019/dave-cross/perl5/ch-2.pl b/challenge-019/dave-cross/perl5/ch-2.pl
new file mode 100644
index 0000000000..70d8035d63
--- /dev/null
+++ b/challenge-019/dave-cross/perl5/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use constant SPACE => ' ';
+use constant SPACE_WIDTH => length(SPACE);
+
+my $line_len = shift || 72; # Old-skool Unix
+
+my $text = join ' ', <DATA>;
+
+my $space_left = $line_len;
+
+for (split /\s+/, $text) {
+ if (SPACE_WIDTH + length() > $space_left) {
+ print "\n$_";
+ $space_left = $line_len - length();
+ } else {
+ print $_ . SPACE;
+ $space_left -= (length() + SPACE_WIDTH);
+ }
+}
+
+print "\n";
+
+__DATA__
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis volutpat,
+ipsum nec luctus dictum, velit nisi sodales dui, ut feugiat risus dolor
+vel metus. Morbi ut pretium velit. Proin ultricies enim magna, at semper
+odio molestie vitae. In hac habitasse platea dictumst. Fusce non sapien
+bibendum ligula pellentesque volutpat in et lectus. Vestibulum ante ipsum
+primis in faucibus orci luctus et ultrices posuere cubilia Curae;
+Vestibulum sodales molestie dignissim.