如何在Perl中对齐表头和行

  • 本文关键字:表头 对齐 Perl perl
  • 更新时间 :
  • 英文 :


这是我用来创建散列(键和值)以打印下面给出的输出的代码。但是,表头并不与每个列对齐。你能帮我修一下吗?我可以用它来帮助我运行这段代码以产生所需的输出。#!/usr/bin/perl使用严格的;使用警告;

     my @pName =
     ( { "Type" => "xxxxxComponent",
         "Name" => "xyz_abc_1234LDO_c7rp1avrusevrmdtop",
         "Rev_Id" => "PROD_2_5",
         "ZZZ_ID" => 99ccccc1,
         "IP_Group" => "ABC RIP xxxxx",
         "Date_Released" => "2015-05-03 6:59:09",
         "AA_Category" => "Hard",
         "Project_IDs" => " "},

        { "Type" => "xxxxxComponent",
          "Name" => "xyz_abc_1234LDO_c7rp1avrusevrmdtop",
          "Rev_Id" => "PROD_2_5",
          "ZZZ_ID" => 99ccccc1,
          "IP_Group" => "ABC RIP xxxxx",
          "Date_Released" => "2015-05-03 6:59:09",
          "AA_Category" => "Hard",
          "Project_IDs" => " "},
      );

         printf "%-8s %-15s %-2s %-8s %-8s %-2s %-8s %-8sn",
         "Type",
         "Name",
         "Rev_Id",
         "IRR_ID",
         "IP_Group",
         "Date_Released",
         "IP_Category",
         "Project_IDs";
  for my $pName (@pName) {
  printf "%-8s %-15s %-2s %-8s %-8s %-2s %-8s %-8sn",
  $pName->{Type},
  $pName->{Name},
  $pName->{Rev_Id},
  $pName->{ZZZ_ID},
  $pName->{IP_Group},
  $pName->{Date_Released},
  $pName->{AA_Category},
  $pName->{Project_IDs};
}
I used this code and printed to console the output but it does not look aligned. I am guessing it is because of this piece of code "%-8s %-15s %-2s %-8s %-8s %-2s %-8s %-8sn" to align the table. Can you please help to choose the number for the output table. I have more than 8 lines of row. I have only copied the code snippet here. 
 Type     Name            Rev_Id IRR_ID   IP_Group Date_Released IP_Category   Project_IDs
 xxxxxComponent xyz_abc_1234LDO_c7rp1avrusevrmdtop PROD_2_5 99034201 SEG xx  Rxxse 2015-05-03 6:59:09 Hard
 xxxxxComponent xyz_abc_1234LDO_c7rp1avrusevrmdtop PROD_2_5 99034201 SEG xx       Rxxse 2015-05-03 6:59:09 Hard
 xxxxxComponent xyz_abc_1234LDO_c7rp1avrusevrmdtop PROD_2_5 99034201 SEG xx Rxxse 2015-05-03 6:59:09 Hard
 Use of uninitialized value in printf at C:Perl64binIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64binIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64binIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64binIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64binIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64binIRR.pl line 90.

这个输出也没有对齐。你能给我一些建议,让我即兴发挥一下吗?

#!/usr/bin/env perl
use strict;
use warnings;
use Text::Table::Tiny;
my @pName = (
    {
        "Type" => "xxxxxComponent",
        "Name" => "xyz_abc_1234LDO_c7rp1avrusevrmdtop",
        "Rev_Id" => "PROD_2_5",
        "ZZZ_ID" => '99ccccc1',
        "IP_Group" => "ABC RIP xxxxx",
        "Date_Released" => "2015-05-03 6:59:09",
        "AA_Category" => "Hard",
        "Project_IDs" => " ",
    },
    {
        "Type" => "xxxxxComponent",
        "Name" => "xyz_abc_1234LDO_c7rp1avrusevrmdtop",
        "Rev_Id" => "PROD_2_5",
        "ZZZ_ID" => '99ccccc1',
        "IP_Group" => "ABC RIP xxxxx",
        "Date_Released" => "2015-05-03 6:59:09",
        "AA_Category" => "Hard",
        "Project_IDs" => " ",
    },
);
my @header = qw(
    Type
    Name
    Rev_Id
    IRR_ID
    IP_Group
    Date_Released
    IP_Category
    Project_IDs
);
print Text::Table::Tiny::table(
    rows => [
        @header,
        map [ @{$_}{@header} ], @pName
    ],
    header_row => 1,
);
输出:

<前>+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+|类型|名称| Rev_Id | IRR_ID | IP_Group | Date_Released | IP_Category | Project_IDs |+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+| xxxxxComponent | xyz_abc_1234LDO_c7rp1avrusevrmdtop | PROD_2_5 | | ABC RIP xxxxx | 2015-05-03 6:59:09 | || xxxxxComponent | xyz_abc_1234LDO_c7rp1avrusevrmdtop | PROD_2_5 | | ABC RIP xxxxx | 2015-05-03 6:59:09 | |+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+%

最新更新