如何在Perl中将表头与行对齐
这是我用来创建散列(键和值)以打印以下给定输出的代码。但是,表格标题并不与每个列对齐。你能帮我修一下吗。我可以使用什么来帮助我运行此代码以生成所需的输出。 #!/usr/bin/perl 严格使用; 使用警告如何在Perl中将表头与行对齐,perl,Perl,这是我用来创建散列(键和值)以打印以下给定输出的代码。但是,表格标题并不与每个列对齐。你能帮我修一下吗。我可以使用什么来帮助我运行此代码以生成所需的输出。 #!/usr/bin/perl 严格使用; 使用警告 my @pName = ( { "Type" => "xxxxxComponent", "Name" => "xyz_abc_1234LDO_c7rp1avrusevrmdtop", "Rev_Id" => "PRO
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.
此输出也没有对齐。你能给我一些即兴表演的技巧吗
多谢各位
#!/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 | | |
+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+%
+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+
|类型|名称|版本| Id | IRR | Id | IP |组|发布日期| IP |类别|项目|Id|
+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+
|xxxxx组件xyz abc 1234LDO c7rp1avrusevrmdtop产品2 5 abc RIP xxxxx 2015-05-03 6:59:09|
|xxxxx组件xyz abc 1234LDO c7rp1avrusevrmdtop产品2 5 abc RIP xxxxx 2015-05-03 6:59:09|
+----------------+------------------------------------+----------+--------+---------------+--------------------+-------------+-------------+%.Post代码其他人可以复制粘贴并运行,无需额外努力。非常感谢您提供的代码。我一定会试试这个。我可以在这上面使用拆分功能吗。对于Perl初学者来说,这看起来很容易理解。谢谢你。关于这一点,我还有另一个问题。我们是否可以在没有库或CPAN模块的情况下实现这一点。你可以阅读模块的源代码来学习如何实现这一点。