Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
使用Perl获得格式化输出的最佳方法_Perl_Formatting - Fatal编程技术网

使用Perl获得格式化输出的最佳方法

使用Perl获得格式化输出的最佳方法,perl,formatting,Perl,Formatting,我想将字符串输出为八列,但我想保持间距不变。我不想在HTML中这样做,但我不知道如何正常地这样做。例如: Something Something Something Something Something Else Else Else Else Else Another Another Another Another Another 行数将每天更改,

我想将字符串输出为八列,但我想保持间距不变。我不想在HTML中这样做,但我不知道如何正常地这样做。例如:

Something    Something     Something     Something     Something
Else         Else          Else          Else          Else
Another      Another       Another       Another       Another
行数将每天更改,但列数将始终保持不变。最好的方法是什么?

将模板中的“11”更改为您需要的任何值。

您可以使用Perl的。 这可能是您不了解的“复杂”方法,很可能是因为它提供了许多选项(左|中|右对齐/填充、前导0等)

:

示例:
格式标准输出=
@>
“左”、“中”、“右”
.
输出:
左中右
这是另一个教程


工作示例:()
#/usr/bin/perl-w
严格使用;
次干道{
我的@arr=(['something 1'、'something 2'、'something 3'、'something 4'、'something 5'、'something 6'、'something 7'、'something 8')
,['else1','else2','else3','else4','else5','else6','else7','else8']
,['another1','another2','another3','another4','another5','another6','another7','another8']
);
对于我的$row(@arr){
格式标准输出=

@ 我会查看格式化,但我会使用(现在是Raku)Form.pm来完成,您可以通过Perl5获得它

这是因为内置格式有许多缺点,例如在编译时静态定义格式(即,动态构建格式可能会很痛苦,通常需要字符串求值),以及一系列其他缺点,例如缺少有用的字段类型(并且不能在Perl 5中扩展这些字段类型).

以下是一个活生生的例子:


很好,谢谢,当我搜索它时,我发现了所有这些复杂的方法。如回避,当事情变得复杂时,通常意味着你有更多的控制/选项。<代码> Prtff <代码>非常强大,但是如果你要打印格式化的报告,请考虑<代码>格式>代码> <代码>格式< /Cord>。页眉/页脚,这使得输出到文件更容易修改。我不需要对页眉或任何东西进行技术处理。只需将内容输出到一张图纸上,然后
printf
很可能就是所需的内容。如果数据在数组中,请参阅我关于如何将数组值作为参数传递的答案-只需传递整个数组y列表,而不是单独指定每个节点。
行数将每天更改,但列数将始终保持不变。
无需动态生成或使用eval。
    printf "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-11s\n",
            $column1, $column2, ..., $column8;
Example:
   format STDOUT =
   @<<<<<<   @||||||   @>>>>>>
   "left",   "middle", "right"
   .
Output:
   left      middle    right
#!/usr/bin/perl -w    

use strict; 

   sub main{    
      my @arr = (['something1','something2','something3','something4','something5','something6','something7','something8']
                ,['else1'     ,'else2'     ,'else3'     ,'else4'     ,'else5'     ,'else6'     ,'else7'     ,'else8'     ]
                ,['another1'  ,'another2'  ,'another3'  ,'another4'  ,'another5'  ,'another6'  ,'another7'  ,'another8'  ]
                );
      
      for my $row (@arr) {
         format STDOUT =
@<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  
         @$row
.
         write;
      }

   }    
       
   main();   
#!/usr/bin/perl

use Perl6::Form;

my @arr = (
    [1..8],
    [9..16],
    [17..24],
);

foreach my $line (@arr) {
    print form
        "{<<<<<} "x8,
        @{$line};
}
1       2       3       4       5       6       7       8
9       10      11      12      13      14      15      16
17      18      19      20      21      22      23      24