有一个变量最多可以包含10行,每行最多可以包含79个字符。超越,每行的第10行和第79个字符,不应该显示任何内容。如何在perl中实现这一点。我不知道如何实现10行。有人可以帮我解决这个问题吗?我没有找到任何相同的解决方案。
计算字符数的代码为:
#!/usr/bin/perl
my $string = "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"
if(length($string) > 79)
{
$string = substr($string,0,79);
}
print "my string is :",$string;
但对于,行怎么检查?以及如何使用行代码来支持它?
答案 0 :(得分:1)
printf
可用于截断字符串:
printf "%.79s", $string; # Limit the string to 79 characters
要仅打印10行,您需要使用某种loop。以下是使用foreach
循环和计数器的示例:
use strict;
use warnings;
my @lines = ...;
my $line_num = 0;
for my $line (@lines) {
last if ++$line_num > 10; # Increment counter and exit loop after 10th line
printf "%.79s", $line;
}
或者,使用splice
只需10行:
for my $line (splice @lines, 10) {
printf "%.79s", $line;
}
答案 1 :(得分:1)
假设您希望字符串在字边界处拆分并重新格式化,以便将内部换行视为空白,并且您希望打印最多10行,每行最多79个字符加上换行符,则此代码似乎执行工作。请注意,问题中的字符串包含单引号和双引号,因此我使用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
如果您的要求与我所说的假设不同,那么显然必须更改代码,但您必须准确说明您的要求。例如,构建一个答案字符串是完全可行的,给定一个长输入,它包含输出而不是打印到标准输出。如果您的拆分要求不同,处理将会有所不同。