aboutsummaryrefslogtreecommitdiff
path: root/challenge-019
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-08-04 21:10:14 +0100
committerGitHub <noreply@github.com>2019-08-04 21:10:14 +0100
commit3355ce1d0967c2d8b9b637458e45a91235544040 (patch)
tree36528c7a4bf71a58687b6485cd2d8186e143e160 /challenge-019
parentdd9658655ea707048039ccfd4d8965e7f5e83db1 (diff)
parent5ba0aa26d652a1473e18518ecf807b6a10bf5d73 (diff)
downloadperlweeklychallenge-club-3355ce1d0967c2d8b9b637458e45a91235544040.tar.gz
perlweeklychallenge-club-3355ce1d0967c2d8b9b637458e45a91235544040.tar.bz2
perlweeklychallenge-club-3355ce1d0967c2d8b9b637458e45a91235544040.zip
Merge pull request #471 from jmaslak/joelle-19-2-1
Solution to 19.2 in P6 and P5
Diffstat (limited to 'challenge-019')
-rwxr-xr-xchallenge-019/joelle-maslak/perl5/ch-2.pl35
-rwxr-xr-xchallenge-019/joelle-maslak/perl6/ch-2.p623
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-019/joelle-maslak/perl5/ch-2.pl b/challenge-019/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..8a95171e2c
--- /dev/null
+++ b/challenge-019/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use v5.22;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+my $col = int( $ARGV[0] // 0 );
+die "Provide column on which to wrap" unless $col;
+die "Too many arguments provided" unless scalar(@ARGV) == 1;
+
+my @lines;
+while ( my $line = <stdin> ) { push @lines, $line }
+
+my (@words) = split /\s+/, join( "", @lines );
+
+my $line = '';
+for my $word (@words) {
+ if ( length($line) + length($word) + 1 > $col ) {
+ say $line;
+ $line = '';
+
+ if ( length($word) > $col ) {
+ die("Words must be <= $col columns long to be wrapped at column $col");
+ }
+ }
+ $line .= ' ' unless length($line) == 0;
+ $line .= $word;
+}
+say $line unless length($line) == 0;
+
diff --git a/challenge-019/joelle-maslak/perl6/ch-2.p6 b/challenge-019/joelle-maslak/perl6/ch-2.p6
new file mode 100755
index 0000000000..da369ae46b
--- /dev/null
+++ b/challenge-019/joelle-maslak/perl6/ch-2.p6
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN(UInt:D $col where * > 0) {
+ my $words = $*IN.words;
+
+ my $line = '';
+ for @$words -> $word {
+ if $line.chars + $word.chars + 1 > $col {
+ say $line;
+ $line = "";
+
+ if $word.chars > $col {
+ die("Words must be ≤ $col columns long to be wrapped at column $col");
+ }
+ }
+ $line ~= " " unless $line.chars == 0;
+ $line ~= $word;
+ }
+ say $line unless $line.chars == 0;
+}
+
+