From 5ba0aa26d652a1473e18518ecf807b6a10bf5d73 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 4 Aug 2019 13:50:31 -0600 Subject: Solution to 19.2 in P6 and P5 --- challenge-019/joelle-maslak/perl5/ch-2.pl | 35 +++++++++++++++++++++++++++++++ challenge-019/joelle-maslak/perl6/ch-2.p6 | 23 ++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100755 challenge-019/joelle-maslak/perl5/ch-2.pl create mode 100755 challenge-019/joelle-maslak/perl6/ch-2.p6 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 = ) { 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; +} + + -- cgit