Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在perl中实现10行和每行79个字符_Regex_Perl - Fatal编程技术网

Regex 如何在perl中实现10行和每行79个字符

Regex 如何在perl中实现10行和每行79个字符,regex,perl,Regex,Perl,有一个变量最多可以有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.

有一个变量最多可以有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;
但对于线路,如何检查?以及如何将其与代码行合并?

可用于截断字符串:

printf "%.79s", $string; # Limit the string to 79 characters
要只打印10行,您需要使用某种类型的打印机。下面是一个使用循环和计数器的示例:

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;
}
或者,使用仅获取10行:

for my $line (splice @lines, 10) {
    printf "%.79s", $line;
}

假设您希望字符串在单词边界处拆分并重新格式化,以便内部换行符被视为空白,并且您希望打印最多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

如果您的需求与我所述的假设不同,那么显然必须更改代码,但您必须准确地陈述您的需求。例如,构建一个答案字符串是完全可行的,给定一个长输入,它包含输出,而不是打印到标准输出。如果您的拆分要求不同,处理过程也会不同。

因此不是为我编写代码的网站。你必须自己尝试,当你被困在某个地方时,在这里陈述你的问题,然后你会得到帮助。我不知道。。可能会先计数到第79个字符,然后再计数\n,这样当您看到数字10时,您就可以删除其他所有内容了?嗯?@dgw:我不知道为什么我会问这个问题。你的变量有一行大约600个字符。你是想把它删去79个字符,还是想把它分成8行?是否应该在单词边界或字符位置进行切分或拆分?是否打算将该值赋回带有内嵌换行符的字符串?或者你只需要打印10行数据,每行最多79个字符加上换行符?为什么你的代码只打印第一行。不是完整的10行。非常感谢你的帮助!你能帮我解决这个问题吗?你是如何定义一条线的?是否要将$string拆分为句子并打印前10个?也许你可以提供问题的预期结果。