使用散列和拆分函数数组在Perl中打印对齐的数据表 [英] Printing an aligned data table in Perl using array of hashes and split function

查看:61
本文介绍了使用散列和拆分函数数组在Perl中打印对齐的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

由于我是Perl的初学者,我很难努力让我的程序正常工作。我正在Perl中编写一个程序来在控制台中打印数据表(带有标题和行对齐)。经过几天的研究后,我发现,可以通过读取数据txt文件并创建一个带有哈希数组的循环来打印表格的标题和行来完成。通过制表符分隔符插入拆分功能。该表实际上将在不安装任何表模块的情况下进行对齐打印。这是我的代码:



Hi Guys,
I'm little struggling to get my program work as I'm a beginner in Perl. I'm writing a program in Perl to print a data table in console (with header and row alignment). For that after couple of days of research I found that, that could be done by reading the data txt file and creating a loop with array of hashes to print the headers and rows of the table. By also inserting a split function by a tab delimiter. The table would actually print with alignment without any table module installed. This is my code:

#!/bin/env perl
use strict;
use warnings;
use autodie;
use Data::Dumper;

# Create a file handle for the input file
my $filename = 'c73p1avfusevrmtop.txt';
open(my $fh, '<:encoding(UTF-8)', $filename);

# print header
my $header = readline $fh;
print $header;             #

# print rows
while (my $row = readline $fh) {
    chomp $row;
    print "$row\n";
}





这是数据txt表:



This is the data txt table:

Type        Name                    Rev Id      ZZZ ID      IP Group        Date Released       AA Category Project IDs
xxxxxComponent  xyz_abc_1234LDO_c7rp1avrusevrmdtop  xxxx_2_5    99ccccc1    ABC RIP xxxxx   2015-05-03 6:59:09  xxxx
xxxxxComponent  xyz_abc_1234LDO_c7rp1avrusevrmdtop  xxxx_2_5    99ccccc1    ABC RIP xxxxx   2015-05-03 6:59:09  xxxx
xxxxxComponent  xyz_abc_1234LDO_c7rp1avrusevrmdtop  xxxx_2_5    99ccccc1    ABC RIP xxxxx   2015-05-03 6:59:09  xxxx



如何编写散列数组和拆分函数以打印此表格并进行对齐。任何想法,提示,帮助将不胜感激?


How do I write the array of hashes and split function to print this table with alignment. Any ideas, tips, help will be greatly appreciated?

推荐答案

filename ='c73p1avfusevrmtop.txt';
open(我的
filename = 'c73p1avfusevrmtop.txt'; open(my


fh,'<:encoding(UTF-8)',
fh, '<:encoding(UTF-8)',


filename);

#print header
my
filename); # print header my


这篇关于使用散列和拆分函数数组在Perl中打印对齐的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆