Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";
}


This is the data txt table:
CSS
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?
Posted
Updated 19-May-15 13:45pm
v2
Comments
Mohibur Rashid 19-May-15 20:55pm    
Simplest thought is:
read all the rows in two dimensional array. go through all row and create a list of maximum char width of each row. Then Loop through your data pad your string with proper spaces and print it.
You can increase your application performance by getting the size while reading your text file.
Also make sure that your data properly trimmed(based on your requirements)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900