Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

How to align table headers to rows in Perl

This is the code I used to creating a hash (keys and values) to print the given below output. However, the table headers do not align with each of the column. Can you help me fix that. What I could use to help me run this code to produce the required output. #!/usr/bin/perl use strict; use warnings;

     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
",
         "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
",
  $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
" 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:Perl64inIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64inIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64inIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64inIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64inIRR.pl line 90.
 Use of uninitialized value in printf at C:Perl64inIRR.pl line 90.

This output is not aligned too. Can you give me some tips to improvise my alignment.

Thank you very much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#!/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,
);

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...