文字列を単語の境界で分割し、内部の改行が空白として扱われるように再フォーマットし、それぞれ最大 79 文字と改行を含む最大 10 行を印刷したい場合、このコードはうまくいくようです。q{}
質問の文字列には一重引用符と二重引用符の両方が含まれているため、文字列を区切るために使用したことに注意してください。
#!/usr/bin/env perl
use strict;
use warnings;
use constant MAXLINELEN => 79;
use constant MAXNUMLINES => 10;
my $string = q{As a describer of life and manners, he must be allowed to stand perhaps the first of the first rank. His humour, which, as Steele observes, is peculiar to himself, is so happily diffused as to give the grace of novelty to domestic scenes and daily occurrences. He never "o'ersteps the modesty of nature," nor raises merriment or wonder by the violation of truth. His figures neither divert by distortion nor amaze by aggravation. He copies life with so much fidelity that he can be hardly said to invent; yet his exhibitions have an air so much original, that it is difficult to suppose them not merely the product of imagination};
sub print_up_to_10_lines_of_79_chars_split_at_words
{
my($string) = @_;
my(@words) = split /\s+/, $string;
my $line_num = 0;
my $line_len = 0;
my $pad = "";
foreach my $word (@words)
{
my $len = length($word);
if ($line_len + length($pad) + $len > MAXLINELEN)
{
last if (++$line_num >= MAXNUMLINES);
print "\n";
$pad = "";
$line_len = 0;
}
printf "%s%s", $pad, $word;
$line_len += length($pad) + $len;
$pad = " ";
}
print "\n";
}
print "First string: (", length($string), ")\n";
print_up_to_10_lines_of_79_chars_split_at_words($string);
$string .= ". $string.";
print "Second string: (", length($string), ")\n";
print_up_to_10_lines_of_79_chars_split_at_words($string);
出力例:
First string: (629)
As a describer of life and manners, he must be allowed to stand perhaps the
first of the first rank. His humour, which, as Steele observes, is peculiar to
himself, is so happily diffused as to give the grace of novelty to domestic
scenes and daily occurrences. He never "o'ersteps the modesty of nature," nor
raises merriment or wonder by the violation of truth. His figures neither
divert by distortion nor amaze by aggravation. He copies life with so much
fidelity that he can be hardly said to invent; yet his exhibitions have an air
so much original, that it is difficult to suppose them not merely the product
of imagination
Second string: (1261)
As a describer of life and manners, he must be allowed to stand perhaps the
first of the first rank. His humour, which, as Steele observes, is peculiar to
himself, is so happily diffused as to give the grace of novelty to domestic
scenes and daily occurrences. He never "o'ersteps the modesty of nature," nor
raises merriment or wonder by the violation of truth. His figures neither
divert by distortion nor amaze by aggravation. He copies life with so much
fidelity that he can be hardly said to invent; yet his exhibitions have an air
so much original, that it is difficult to suppose them not merely the product
of imagination. As a describer of life and manners, he must be allowed to stand
perhaps the first of the first rank. His humour, which, as Steele observes, is
要件が私が述べた仮定と異なる場合は、明らかにコードを変更する必要がありますが、要件を正確に記述する必要があります。たとえば、長い入力が与えられた場合、標準出力に出力するのではなく、出力を含む応答文字列を作成することは完全に実行可能です。分割要件が異なる場合、処理は異なります。