我正在用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
我将如何实现这一目标?
答案 0 :(得分: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
答案 1 :(得分:1)
format
是真正的方法。
以下是使用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
"{<<<<}{[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[} {<<}", @$_ ;
}
答案 2 :(得分:0)
您可以使用regexp来包装文本。
由于您是逐行打印的,
你可以采用标量变量中的每一行并检查该标量变量的长度并插入换行符\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;
}
答案 3 :(得分:0)
如果你想要一些非常基本/特定的东西(不健壮),每隔四个逗号强制换行:
在printf
:
$$f{item} =~ s/(,[^,]+,[^,]+,[^,]+,)/$1\n/g;
最佳选择可能是使用大卫建议的perlform (5.8.8 for you)
答案 4 :(得分: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 ;
}
对所有回答我问题的人,非常感谢你的帮助,我非常感谢!