1

レポートを表形式で送信するためにPerlで書いていますが、データの入力が長すぎる場合は常に問題が発生します。行を複数に折り返すことができるようにコードを書く方法はありますか?これが私のコードと出力です。ありがとう!

printf "\n%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;
printf "%-${max1}s %-${max2}s %-15s\n", "Code", "Item", "Group" ;
printf "%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;

foreach my $f (@{$rpt_ptr}) {
printf "%-${max1}s %-${max2}s %-15s\n", "$$f{code}", "$$f{item}", "$$f{group}", 
}

アイテムリストが長すぎる場合、出力テーブルが長すぎます。つまり、次のようになります。

====  =====                                                                                =====      

Code  Item                                                                                 Group

====  =====                                                                                =====

A1011 aaaaaa, bbbbb, ccccc, ddddd, eeeee, fffff, ggggg, hhhhh, iiiii, jjjjjj, kkkkk, llll    B
    B101  cccccc                                                                                 A

私は次のようなテーブルを出すことができるかどうかについてアスペクトされています:

====  =====                                                                                =====      
Code  Item                                                                                 Group
====  =====                                                                                =====
A1011 aaaaaa, bbbbb, ccccc, ddddd,                                                           B
      eeeee, fffff, ggggg, hhhhh,   
      iiiii, jjjjjj, kkkkk, llll
B101  cccccc                                                                                 A

どうすればそれを達成できますか?

4

5 に答える 5

3

Perlは元々、テキストファイルをフォーマットするために作成されました。これには、指定した方法で行を折り返す機能など、かなり興味深いフォーム生成機能が含まれています。私はそれが長い間使われているのを見たことがありませんが、それでも言語の一部です。

これは、 perlformの下のPerldocにあります。


#! /usr/bin/env perl

use strict;
use warnings;
use feature qw(say);

my ($invoice, $description, $amount);
while ( chomp ( my $line = <DATA> ) ) {
    ( $invoice, $description, $amount ) = split /:/ => $line;
    write;
}

format STDOUT_TOP =
Invoice   Description       Amount
=======   ===========       =======
.
format STDOUT = 
@<<<<<<   ^<<<<<<<<<<<<<<   @###.##
$invoice, $description,     $amount
          ^<<<<<<<<<<<<<<
          $description
          ^<<<<<<<<<<<<<<
          $description
          ^<<<<<<<<<<<<<<
          $description
          ^<<<<<<<<<<<<<<
          $description
.
__DATA__
One:This line contans a lot of text that I have to process and I have no idea what it is:2.30
Two:Here's another line that contains lots of data that won't fit on a single line:4.40
Three:And one more line of extra long data that may or may not fit on a single line:5.10

出力

Invoice   Description       Amount
=======   ===========       =======
One       This line            2.30
          contans a lot
          of text that I
          have to process
          and I have no
Two       Here's another       4.40
          line that
          contains lots
          of data that
          won't fit on a
Three     And one more         5.10
          line of extra
          long data that
          may or may not
          fit on a single
于 2013-01-17T03:16:42.037 に答える
1

formatsは本当に行く方法です。

Perl6::Formを使用した解決策は次のとおりです。

use Perl6::Form;

my @data = (
    ['A1011', 'aaaaaa, bbbbb, ccccc, ddddd, eeeee, fffff, ggggg, hhhhh, iiiii, jjjjjj, kkkkk, llll', 'B'],
    ['B101', 'cccccc', ''],
);

print form
    "====  =====                                              =====",
    "Code  Item                                               Group",
    "====  =====                                              =====";

foreach (@data) {
    print form
        "{<<<<}{[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[}             {<<}", @$_ ;
}
于 2013-01-17T07:58:42.657 に答える
0

このテキストの折り返しには正規表現を使用できます。ファイルを1行ずつ印刷しているので、スカラー変数のすべての行を取得し、そのスカラー変数の長さを確認して、改行文字を挿入できます\n

お気に入り:

    $var = "\n%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;
    $var .= "%-${max1}s %-${max2}s %-15s\n", "Code", "Item", "Group" ;
    $var .= "%-${max1}s %-${max2}s %-15s\n", "====", "====", "=====" ;

    ## Adding new line character after 20 characters.
    my $startIndex = 0;

    while( ($startIndex < length($var) && (length($var) > 20) ) {
     print substr($var, $startIndex, 20)  . "\n";
     $startIndex = $startIndex + 20;
    }
于 2013-01-17T05:44:06.247 に答える
0

非常に基本的/特定の(堅牢ではない)ものが必要な場合は、4つおきのコンマで改行を強制します。

printf:の前のループで

$$f{item} =~ s/(,[^,]+,[^,]+,[^,]+,)/$1\n/g;

Davidが提案したように、おそらく最良のオプションはperlform(5.8.8)を使用することです。

于 2013-01-21T03:19:25.453 に答える
0

私はついに次のコードでそれを成し遂げました、共有するためにここに投稿されました:-

foreach my $f (@{$rpt_ptr}) {
#   printf "%-${max1}s %-${max2}s %-15s\n", "$$f{code}", "$$f{item}", "$$f{group}", 
format STDOUT_TOP =
===============     =========================     ========= 
Code                 Item                     Group
===============     =========================     ========= 
.

format STDOUT =
@<<<<<<<<<<<<<<     ^<<<<<<<<<<<<<<<<<<<<<<<<<     @<<<<<<<<
$$f{code}               $$f{item}                                              $$f{group} 
~~                ^<<<<<<<<<<<<<<<<<<<<<<<<
                   $$f{item}
.
write ;
}

私の質問に答えてくれたすべての人に、あなたの助けに大いに感謝します、私はそれを大いに感謝します!

于 2013-01-22T07:00:51.830 に答える