diff options
| author | Joelle Maslak <jmaslak@antelope.net> | 2019-08-04 13:50:31 -0600 |
|---|---|---|
| committer | Joelle Maslak <jmaslak@antelope.net> | 2019-08-04 13:50:31 -0600 |
| commit | 5ba0aa26d652a1473e18518ecf807b6a10bf5d73 (patch) | |
| tree | a9c42bd83fd6a3a4bf00a5498f931878b54d8be0 | |
| parent | a933b2dc940659b62cac2f180df777a90d16ac60 (diff) | |
| download | perlweeklychallenge-club-5ba0aa26d652a1473e18518ecf807b6a10bf5d73.tar.gz perlweeklychallenge-club-5ba0aa26d652a1473e18518ecf807b6a10bf5d73.tar.bz2 perlweeklychallenge-club-5ba0aa26d652a1473e18518ecf807b6a10bf5d73.zip | |
Solution to 19.2 in P6 and P5
| -rwxr-xr-x | challenge-019/joelle-maslak/perl5/ch-2.pl | 35 | ||||
| -rwxr-xr-x | challenge-019/joelle-maslak/perl6/ch-2.p6 | 23 |
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; +} + + |
