【问题标题】:How to format output in Perl?如何在 Perl 中格式化输出?
【发布时间】:2012-11-07 14:41:34
【问题描述】:

我有一个负责格式化输出的 Perl 代码。问题是我需要计算要在每个字符串中的一个特定字符之前放置的正确标签数(假设它是 /)。因此,我不想得到不同长度的行,而是得到固定长度的字符串(但尽可能短,使用最长的作为最大值)。我应该如何处理这个?

输出示例(格式被注释掉,只是原始数组):

Long title with some looong words / short text
Another title / another short text

我需要这样的:

title with some looong words / short text
Different title              / another short text

这是我的第一次尝试,但它不涉及在一个特定字符后使用制表符。

my $text = Text::Format->new;

$text->rightAlign(1);
$text->columns(65);
$text->tabstop(4);

$values[$i] = $text->format(@values[$i]);

【问题讨论】:

  • 1.迭代所有字符串以找到最大长度 2。将制表符插入其他字符串以匹配先前找到的最大长度 3...? 4. 利润
  • 请添加一些示例数据。很难理解你想做什么。

标签: perl text formatting


【解决方案1】:

在 Perl 中有两种普遍接受的格式化文本的方式,因此列排列:

  • 使用printfsprintf。这可能是最常见的方式。
  • 使用Perl Format 功能。您定义您想要的线条外观,然后使用write 打印成该格式。

我已经好几年没见过有人使用 Perl 格式规范的东西了。这是 Perl 3.x 鼎盛时期的一个重要功能,因为 Perl 主要用作超级 awk 替代语言(Practical Extraction 和 R输出L语言)。但是,机制仍然存在,我相信它会很有趣。


现在是查询的第二部分。你真的不只是希望文本被格式化,你希望它被用标签格式化

幸运的是,Perl 有一个名为 Text::Tabs 的内置模块。它是那些完成一两个晦涩任务但做得很好的模块之一。

实际上,Text::Tabs 并没有很好地处理这两项任务,但做得很好。

使用sprintf 或内置的 Perl 格式化功能生成您的报告。将整个报告保存在一个数组中。它必须是一个数组,因为Text::Tabs 不处理 NL。然后使用unexpand 函数将该数组中的空格替换为另一个带有制表符的数组。

WORD 'O WARNINGText::Tabs 做了一些非常顽皮的事情:它在未经您许可的情况下将变量 $tabstop 导入您的程序。如果您使用名为$tabstop 的变量,则必须更改它。您可以将$tabstop 设置为制表符代表的空格数。默认设置为 8。

【讨论】:

    【解决方案2】:

    Text::Table 模块可能会对您有所帮助。

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    use Text::Table;
    
    my @array = ('Long title with some looong words', 'short text',
                 'Another title',                     'another short text');
    
    my $table = Text::Table::->new(q(), \' / ', q()); # Define the separator.
    $table->add(shift @array, shift @array) while @array;
    print $table;
    

    【讨论】:

      【解决方案3】:

      根据我在文档 Text::Format 中可以找到的内容,仅进行段落格式设置。你想要的是表格格式。基本方法是将数据拆分为每行和每列的文本矩阵,找到每列的最长元素,并使用它来输出固定宽度的数据。例如:

      my @values = (
          'Long title with some looong words / short text',
          'Another title / another short text',
      );
      
      # split values into per-column text
      my @text = map { [ split '\s*(/)\s*', $_ ] } @values;
      
      # find the maximum width of each column
      my @width;
      foreach my $line (@text) {
          for my $i (0 .. $#$line) {
              no warnings 'uninitialized';
              my $l = length $line->[$i];
              $width[$i] = $l if $l > $width[$i];
          }
      }
      
      # build a format string using column widths
      my $format = join ' ', map { "%-${_}s" } @width;
      $format .= "\n";
      
      # print output in columns
      printf $format, @$_ foreach @text;
      

      这会产生以下内容:

      Long title with some looong words / short text        
      Another title                     / another short text
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-19
        • 2014-01-28
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多