如何将表头与Perl中的行对齐

时间:2015-05-15 22:11:31

标签: 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 %-8s\n",
         "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 %-8s\n",
  $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 %-8s\n" 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:\Perl64\bin\IRR.pl line 90.
 Use of uninitialized value in printf at C:\Perl64\bin\IRR.pl line 90.
 Use of uninitialized value in printf at C:\Perl64\bin\IRR.pl line 90.
 Use of uninitialized value in printf at C:\Perl64\bin\IRR.pl line 90.
 Use of uninitialized value in printf at C:\Perl64\bin\IRR.pl line 90.
 Use of uninitialized value in printf at C:\Perl64\bin\IRR.pl line 90.

此输出也未对齐。你可以给我一些即兴表演的提示吗。

非常感谢。

1 个答案:

答案 0 :(得分:3)

#!/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,
);

输出:

+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+
| Type           | Name                               | 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 |             |             |
+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+%