Perl脚本 - 在终端输出中实时创建的表

时间:2018-04-24 12:13:28

标签: perl

我的脚本在终端输出的不同行上打印数据,如下所示:

Name: Paul Surname: Doe Age:34
Name: Paulo Surname: Doeson Age:28 
Name: Paulinho Surname: Docsone Age:5
Name: Paulmen Surname: Doee Age:12
Name: Paulchen Surname: Dooe Age:31

我希望将所有数据分成列并在条形图中显示,如下所示:

Name: Paul     Surname: Doe     Age:34
Name: Paulo    Surname: Doeson  Age:28
Name: Paulinho Surname: Docsone Age:5 
Name: Paulmen  Surname: Doee    Age:12 
Name: Paulchen Surname: Dooe    Age:31 

我注意到输出无法缓冲或存储。我想在运行脚本时实时实现这一点。有没有任何技巧来实现这一目标?比如在HTML表格中指定单元格的宽度?

修改
真的示例代码:

@arr = q(Name: Paul Surname: Doe Age:34, Name: Paulo Surname: Doeson Age:28, Name: Paulinho Surname: Docsone Age:5, Name: Paulmen Surname: Doee Age:12, Name: Paulchen Surname: Dooe Age:31);

foreach (@arr) {
  # here should be our trick to creating columns
  print $_ # should be print element in columns (without buffering). print now, not after loop end. 
}

1 个答案:

答案 0 :(得分:2)

我想到的第一件事是Perl formats。它是内置的,并为您提供了大量格式化输出的方法。看看吧。