-
Notifications
You must be signed in to change notification settings - Fork 320
/
ch-1.pl
executable file
·42 lines (36 loc) · 1.06 KB
/
ch-1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/perl
use 5.030;
use warnings;
sub wordWrap {
my ($paragraph, $lineWidth) = @_;
my $spaceLeft = $lineWidth + 1;
while ( $paragraph =~ /\G (?<word> \S+)(\s+)? /gcx ) {
my $wordWidth = length $+{word};
if ($wordWidth + 1 > $spaceLeft) {
print "\n";
$spaceLeft = $lineWidth - $wordWidth;
} else {
$spaceLeft -= ($wordWidth + 1);
}
print "$+{word} ";
}
print "\n";
}
my $text = <<'-TEXT-';
All he could think about was how it would all end. There was
still a bit of uncertainty in the equation, but the basics
were there for anyone to see. No matter how much he tried to
see the positive, it wasn't anywhere to be seen. The end was
coming and it wasn't going to be pretty.
-TEXT-
$text =~ s/\n/ /msgx;
$text =~ s/\ $//msgx;
my $newtext;
my @sentences = split q{\.}, $text;
for my $sentence (@sentences) {
my @words = split /\ +/, $sentence;
@words = sort { uc $a cmp uc $b } @words;
$newtext .= join q{ }, @words;
$newtext .= q{. };
}
wordWrap($newtext, 60);