-1

有一个变量最多可以有 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;

但对于,线路如何检查?以及如何使用行代码来俱乐部它?

4

2 回答 2

1

printf可用于截断字符串:

printf "%.79s", $string; # Limit the string to 79 characters

要仅打印 10 行,您需要使用某种循环。这是一个使用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;
}
于 2013-10-31T10:26:49.737 回答
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

如果您的要求与我所说的假设不同,那么显然必须更改代码,但您必须准确地说明您的要求。例如,在给定长输入的情况下,构建一个包含输出而不是打印到标准输出的答案字符串是完全可行的。如果您的拆分要求不同,则处理方式会有所不同。

于 2013-10-31T11:08:20.543 回答