-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

文字列を単語の境界で分割し、内部の改行が空白として扱われるように再フォーマットし、それぞれ最大 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

要件が私が述べた仮定と異なる場合は、明らかにコードを変更する必要がありますが、要件を正確に記述する必要があります。たとえば、長い入力が与えられた場合、標準出力に出力するのではなく、出力を含む応答文字列を作成することは完全に実行可能です。分割要件が異なる場合、処理は異なります。

于 2013-10-31T11:08:20.543 に答える