blob: 8470a103e795a6d07e4e76522b2c2e351f703af4 (
plain)
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
|
#!/usr/bin/env perl6
#
# Wrap paragraph provided on stdin using greedy algorithm
#
my @words = do { slurp.words };
my $word_width = 0;
my $line_width = 20;
my $space_left = $line_width;
my $space_width = 1;
for @words -> $w {
$word_width = $w.chars;
if ( ($space_left < $line_width) && ($space_width + $word_width <= $space_left) )
{
printf " $w";
$space_left -= ( $space_width + $word_width );
}
else
{
printf "{ $space_left == $line_width ?? "" !! "\n" }$w";
$space_left = $line_width - $word_width;
};
}
printf "\n";
|