Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Align columns in a table

by ovedpo15 (Pilgrim)
on Aug 28, 2021 at 20:34 UTC ( #11136164=perlquestion: print w/replies, xml ) Need Help??

ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I'm trying to print the coverage table. I was trying to use the Data::Table module:
my @table_header = ("Package", "Coverage", "Line", "Method", "Branch") +; my @data_table; foreach my $package (sort(keys(%{$result}))) { my $instruction_package_missed = $result->{$package}{$HEADER_INST +RUCTION_MISSED}; my $instruction_package_covered = $result->{$package}{$HEADER_INST +RUCTION_COVERED}; my $branch_package_missed = $result->{$package}{$HEADER_BRAN +CH_MISSED}; my $branch_package_covered = $result->{$package}{$HEADER_BRAN +CH_COVERED}; my $line_package_missed = $result->{$package}{$HEADER_LINE +_MISSED}; my $line_package_covered = $result->{$package}{$HEADER_LINE +_COVERED}; my $method_package_missed = $result->{$package}{$HEADER_METH +OD_MISSED}; my $method_package_covered = $result->{$package}{$HEADER_METH +OD_COVERED}; my $instructions_package_total = $instruction_package_missed + $in +struction_package_covered; my $branchs_package_total = $branch_package_missed + $branch_ +package_covered; my $lines_package_total = $line_package_missed + $line_pack +age_covered; my $methods_package_total = $method_package_missed + $method_ +package_covered; my $instruction_package_percentage = calculate_percentage($instruc +tion_package_covered,$instructions_package_total); my $branch_package_percentage = calculate_percentage($branch_ +package_covered,$branchs_package_total); my $line_package_percentage = calculate_percentage($line_pa +ckage_covered,$lines_package_total); my $method_package_percentage = calculate_percentage($method_ +package_covered,$methods_package_total); my $instruction_package_summary = "$instruction_package_percentage +\%"; my $branch_package_summary = "$branch_package_percentage\% ($ +branch_package_covered/$branchs_package_total)"; my $line_package_summary = "$line_package_percentage\% ($li +ne_package_covered/$lines_package_total)"; my $method_package_summary = "$method_package_percentage\% ($ +method_package_covered/$methods_package_total)"; my @coverage_line = ($package, $instruction_package_summary, $line +_package_summary, $method_package_summary, $branch_package_summary); push(@data_table,\@coverage_line); } my $table = Data::Table->new(\@data_table, \@table_header, 0); print $table->tsv."\n";
But it print it:
Package Coverage Line Method Branch com.gandu.base.alltests 90% 91% (20/22) 50% (1/2) 0 +% (0/0) com.gandu.base.commons 47% 43% (579/1336) 34% (73/213) 4 +1% (111/270) com.gandu.base.commons.status 27% 27% (103/388) 46% (25/5 +4) 8% (5/60) com.gandu.base.commons.test 94% 84% (62/74) 85% (11/13) 1 +00% (6/6) com.gandu.base.commons.validchecker 90% 90% (9/10) 50% + (1/2) 0% (0/0) com.gandu.base.commons.validchecker.arghandler 12% 8% (115/140 +4) 8% (8/99) 0% (0/350) com.gandu.base.commons.validchecker.centralarea 26% 37% (111/29 +7) 65% (17/26) 56% (47/84) com.gandu.base.commons.validchecker.testtag 85% 82% (613/74 +8) 82% (93/114) 53% (90/170) com.gandu.base.consoleindependentcommons 91% 91% (79/87) 8 +2% (14/17) 90% (18/20) com.gandu.base.disk 80% 79% (70/89) 76% (16/21) 75% (27/3 +6) com.gandu.base.disk.test 2% 3% (3/98) 7% (1/14) 0 +% (0/0) com.gandu.base.flow 39% 37% (479/1281) 41% (131/321) 6% (21/33 +6) com.gandu.base.flow.parser 31% 34% (468/1363) 34% (48/141) 1 +5% (51/348) com.gandu.base.helpcontent 0% 0% (0/6) 0% (0/4) 0 +% (0/0) com.gandu.base.datafeeder 0% 0% (0/144) 0% (0/23) + 0% (0/66) com.gandu.base.datafeeder.test 3% 4% (3/82) 8% (1/12) + 0% (0/20) com.gandu.base.preferencefiles 62% 55% (505/915) 52% (15/2 +9) 21% (42/204) com.gandu.base.preferences 50% 43% (1139/2620) 28% (138/500) 6 +% (38/688) com.gandu.base.report 19% 22% (439/1954) 61% (46/76) 4 +7% (105/224) com.gandu.base.report.emailreport 91% 88% (244/278) 93% (26/2 +8) 67% (28/42) com.gandu.base.report.test 86% 86% (6/7) 50% (1/2) 0 +% (0/0) com.gandu.base.managerexec 28% 26% (1101/4209) 43% (90/208) 34 +% (200/593) com.gandu.base.managerexec.runmanager 18% 18% (344/1913) 23 +% (46/200) 19% (122/638) com.gandu.base.managerexec.rununitexecutor 15% 18% (907/5149) 40 +% (115/291) 6% (61/1062) com.gandu.base.testmanager 95% 89% (117/131) 78% (21/27) 8 +2% (28/34) com.gandu.base.testmanager.housekeeping 72% 69% (1044/1513) 6 +8% (116/171) 50% (130/260) com.gandu.base.util 10% 11% (48/450) 13% (11/82) 12% (13/1 +07) com.gandu.base.util.test 81% 80% (4/5) 50% (1/2) 0 +% (0/0)
I'm trying to print the table aligned so each column will be seperated. I could not find a method in Data::Table but I may missed it. I also noticed that Text::Table and Text::Table::Tiny, can be used but unfortunately those modules are not installed by IT in the central area so I prefer to use an installed module or implementation of this idea. What would be a good way to solve it?
Edit: I'm looking to a similar solution to the output of Python's pandas.

Replies are listed 'Best First'.
Re: Align columns in a table
by tybalt89 (Prior) on Aug 29, 2021 at 02:59 UTC

    Just do it yourself...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11136164 use warnings; my @data = map [ /\S+(?: \(\S+)?/g ], <DATA>; my @wide; for my $line ( @data ) { $wide[$_] |= '' . $line->[$_] for 0 .. $#$line; } $_ = sprintf "%%-%ds", length for @wide; my $format = join(' ', @wide) . "\n"; printf $format, @$_ for @data; __DATA__ Package Coverage Line Method Branch com.gandu.base.alltests 90% 91% (20/22) 50% (1/2) 0 +% (0/0) com.gandu.base.commons 47% 43% (579/1336) 34% (73/213) 4 +1% (111/270) com.gandu.base.commons.status 27% 27% (103/388) 46% (25/5 +4) 8% (5/60) com.gandu.base.commons.test 94% 84% (62/74) 85% (11/13) 1 +00% (6/6) com.gandu.base.commons.validchecker 90% 90% (9/10) 50% + (1/2) 0% (0/0) com.gandu.base.commons.validchecker.arghandler 12% 8% (115/140 +4) 8% (8/99) 0% (0/350) com.gandu.base.commons.validchecker.centralarea 26% 37% (111/29 +7) 65% (17/26) 56% (47/84) com.gandu.base.commons.validchecker.testtag 85% 82% (613/74 +8) 82% (93/114) 53% (90/170) com.gandu.base.consoleindependentcommons 91% 91% (79/87) 8 +2% (14/17) 90% (18/20) com.gandu.base.disk 80% 79% (70/89) 76% (16/21) 75% (27/3 +6) com.gandu.base.disk.test 2% 3% (3/98) 7% (1/14) 0 +% (0/0) com.gandu.base.flow 39% 37% (479/1281) 41% (131/321) 6% (21/33 +6) com.gandu.base.flow.parser 31% 34% (468/1363) 34% (48/141) 1 +5% (51/348) com.gandu.base.helpcontent 0% 0% (0/6) 0% (0/4) 0 +% (0/0) com.gandu.base.datafeeder 0% 0% (0/144) 0% (0/23) + 0% (0/66) com.gandu.base.datafeeder.test 3% 4% (3/82) 8% (1/12) + 0% (0/20) com.gandu.base.preferencefiles 62% 55% (505/915) 52% (15/2 +9) 21% (42/204) com.gandu.base.preferences 50% 43% (1139/2620) 28% (138/500) 6 +% (38/688) com.gandu.base.report 19% 22% (439/1954) 61% (46/76) 4 +7% (105/224) com.gandu.base.report.emailreport 91% 88% (244/278) 93% (26/2 +8) 67% (28/42) com.gandu.base.report.test 86% 86% (6/7) 50% (1/2) 0 +% (0/0) com.gandu.base.managerexec 28% 26% (1101/4209) 43% (90/208) 34 +% (200/593) com.gandu.base.managerexec.runmanager 18% 18% (344/1913) 23 +% (46/200) 19% (122/638) com.gandu.base.managerexec.rununitexecutor 15% 18% (907/5149) 40 +% (115/291) 6% (61/1062) com.gandu.base.testmanager 95% 89% (117/131) 78% (21/27) 8 +2% (28/34) com.gandu.base.testmanager.housekeeping 72% 69% (1044/1513) 6 +8% (116/171) 50% (130/260) com.gandu.base.util 10% 11% (48/450) 13% (11/82) 12% (13/1 +07) com.gandu.base.util.test 81% 80% (4/5) 50% (1/2) 0 +% (0/0)

    Outputs:

    Package Coverage Line + Method Branch com.gandu.base.alltests 90% 91% (20/22) + 50% (1/2) 0% (0/0) com.gandu.base.commons 47% 43% (579/13 +36) 34% (73/213) 41% (111/270) com.gandu.base.commons.status 27% 27% (103/38 +8) 46% (25/54) 8% (5/60) com.gandu.base.commons.test 94% 84% (62/74) + 85% (11/13) 100% (6/6) com.gandu.base.commons.validchecker 90% 90% (9/10) + 50% (1/2) 0% (0/0) com.gandu.base.commons.validchecker.arghandler 12% 8% (115/140 +4) 8% (8/99) 0% (0/350) com.gandu.base.commons.validchecker.centralarea 26% 37% (111/29 +7) 65% (17/26) 56% (47/84) com.gandu.base.commons.validchecker.testtag 85% 82% (613/74 +8) 82% (93/114) 53% (90/170) com.gandu.base.consoleindependentcommons 91% 91% (79/87) + 82% (14/17) 90% (18/20) com.gandu.base.disk 80% 79% (70/89) + 76% (16/21) 75% (27/36) com.gandu.base.disk.test 2% 3% (3/98) + 7% (1/14) 0% (0/0) com.gandu.base.flow 39% 37% (479/12 +81) 41% (131/321) 6% (21/336) com.gandu.base.flow.parser 31% 34% (468/13 +63) 34% (48/141) 15% (51/348) com.gandu.base.helpcontent 0% 0% (0/6) + 0% (0/4) 0% (0/0) com.gandu.base.datafeeder 0% 0% (0/144) + 0% (0/23) 0% (0/66) com.gandu.base.datafeeder.test 3% 4% (3/82) + 8% (1/12) 0% (0/20) com.gandu.base.preferencefiles 62% 55% (505/91 +5) 52% (15/29) 21% (42/204) com.gandu.base.preferences 50% 43% (1139/2 +620) 28% (138/500) 6% (38/688) com.gandu.base.report 19% 22% (439/19 +54) 61% (46/76) 47% (105/224) com.gandu.base.report.emailreport 91% 88% (244/27 +8) 93% (26/28) 67% (28/42) com.gandu.base.report.test 86% 86% (6/7) + 50% (1/2) 0% (0/0) com.gandu.base.managerexec 28% 26% (1101/4 +209) 43% (90/208) 34% (200/593) com.gandu.base.managerexec.runmanager 18% 18% (344/19 +13) 23% (46/200) 19% (122/638) com.gandu.base.managerexec.rununitexecutor 15% 18% (907/51 +49) 40% (115/291) 6% (61/1062) com.gandu.base.testmanager 95% 89% (117/13 +1) 78% (21/27) 82% (28/34) com.gandu.base.testmanager.housekeeping 72% 69% (1044/1 +513) 68% (116/171) 50% (130/260) com.gandu.base.util 10% 11% (48/450 +) 13% (11/82) 12% (13/107) com.gandu.base.util.test 81% 80% (4/5) + 50% (1/2) 0% (0/0)
      That is awesome, thank you!
      The syntax is a bit hard for me to read. How to assign the table into a string and print it at the end? Tried:
      # $_ = sprintf "%%-%ds", length for @wide; $result = "%%-%ds", length for @wide;
      Thank you again!
        #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11136164 use warnings; my @data = map [ /\S+(?: \(\S+)?/g ], <DATA>; my @wide; for my $line ( @data ) { $wide[$_] |= '' . $line->[$_] for 0 .. $#$line; } $_ = sprintf "%%-%ds", length for @wide; my $format = join(' ', @wide) . "\n"; #printf $format, @$_ for @data; # previous code my $table = join '', map { sprintf $format, @$_ } @data; # new combine + code print $table;
Re: Align columns in a table
by choroba (Archbishop) on Aug 31, 2021 at 09:05 UTC
    Use Text::Table.
    #! /usr/bin/perl use strict; use warnings; use Text::Table; my $table = 'Text::Table'->new; while (<DATA>) { my @columns = split / +(?!\()/; $table->add(@columns); } print $table; __DATA__ Package Coverage Line Method Branch com.gandu.base.alltests 90% 91% (20/22) 50% (1/2) 0 +% (0/0) ...

    Update: I used split to process the output of your script to keep the parentheses together with the percentages, but if you use the module and populate the table directly with the values, it gets even simpler. Instead of

    push(@data_table,\@coverage_line);
    you'd do
    $table->add(@coverage_line);
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Align columns in a table
by BillKSmith (Monsignor) on Aug 28, 2021 at 21:38 UTC
    I am reluctant to recommend the perl function write for anything, but it may be exactly what you need (no module required at all).
    Bill
Re: Align columns in a table
by dbuckhal (Hermit) on Aug 31, 2021 at 13:31 UTC

    just for fun, here is my contribution of a write version using format, possibly hinted by BillKSmith

    the sprintf was a bit of a pain, but you get the idea... haha

    #!/usr/local/bin/perl use strict; use warnings; chomp ( my $first = <DATA> ); my @header = split /\s+/, $first; my $body = ""; $body .= sprintf "| %-50s | %10s | %4s %-11s| %4s %-11s| %4s %-11s|\n" +, split /\s+/, $_ while <DATA>; format STDOUT = @||||||||||||||||||||||||||||||||||||||||||||||||||||||||@||||||||||@ +|||||||||||||||||@|||||||||||||||||@||||||||||||||||| @header ------------------------------------------------------------------ +------------------------------------------------------- ^* $body ~~ ^* $body ------------------------------------------------------------------ +------------------------------------------------------- . write STDOUT; __DATA__ Package Coverage Line Method Branch com.gandu.base.alltests 90% 91% (20/22) 50% (1/2) 0 +% (0/0) com.gandu.base.commons 47% 43% (579/1336) 34% (73/213) 4 +1% (111/270) com.gandu.base.commons.status 27% 27% (103/388) 46% (25/5 +4) 8% (5/60) com.gandu.base.commons.test 94% 84% (62/74) 85% (11/13) 1 +00% (6/6) com.gandu.base.commons.validchecker 90% 90% (9/10) 50% + (1/2) 0% (0/0) com.gandu.base.commons.validchecker.arghandler 12% 8% (115/140 +4) 8% (8/99) 0% (0/350) com.gandu.base.commons.validchecker.centralarea 26% 37% (111/29 +7) 65% (17/26) 56% (47/84) com.gandu.base.commons.validchecker.testtag 85% 82% (613/74 +8) 82% (93/114) 53% (90/170) com.gandu.base.consoleindependentcommons 91% 91% (79/87) 8 +2% (14/17) 90% (18/20) com.gandu.base.disk 80% 79% (70/89) 76% (16/21) 75% (27/3 +6) com.gandu.base.disk.test 2% 3% (3/98) 7% (1/14) 0 +% (0/0) com.gandu.base.flow 39% 37% (479/1281) 41% (131/321) 6% (21/33 +6) com.gandu.base.flow.parser 31% 34% (468/1363) 34% (48/141) 1 +5% (51/348) com.gandu.base.helpcontent 0% 0% (0/6) 0% (0/4) 0 +% (0/0) com.gandu.base.datafeeder 0% 0% (0/144) 0% (0/23) + 0% (0/66) com.gandu.base.datafeeder.test 3% 4% (3/82) 8% (1/12) + 0% (0/20) com.gandu.base.preferencefiles 62% 55% (505/915) 52% (15/2 +9) 21% (42/204) com.gandu.base.preferences 50% 43% (1139/2620) 28% (138/500) 6 +% (38/688) com.gandu.base.report 19% 22% (439/1954) 61% (46/76) 4 +7% (105/224) com.gandu.base.report.emailreport 91% 88% (244/278) 93% (26/2 +8) 67% (28/42) com.gandu.base.report.test 86% 86% (6/7) 50% (1/2) 0 +% (0/0) com.gandu.base.managerexec 28% 26% (1101/4209) 43% (90/208) 34 +% (200/593) com.gandu.base.managerexec.runmanager 18% 18% (344/1913) 23 +% (46/200) 19% (122/638) com.gandu.base.managerexec.rununitexecutor 15% 18% (907/5149) 40 +% (115/291) 6% (61/1062) com.gandu.base.testmanager 95% 89% (117/131) 78% (21/27) 8 +2% (28/34) com.gandu.base.testmanager.housekeeping 72% 69% (1044/1513) 6 +8% (116/171) 50% (130/260) com.gandu.base.util 10% 11% (48/450) 13% (11/82) 12% (13/1 +07) com.gandu.base.util.test 81% 80% (4/5) 50% (1/2) 0 +% (0/0)
    __output__
    Package Coverage + Line Method Branch ------------------------------------------------------------------ +------------------------------------------------------- | com.gandu.base.alltests | 90% +| 91% (20/22) | 50% (1/2) | 0% (0/0) | | com.gandu.base.commons | 47% +| 43% (579/1336) | 34% (73/213) | 41% (111/270) | | com.gandu.base.commons.status | 27% +| 27% (103/388) | 46% (25/54) | 8% (5/60) | | com.gandu.base.commons.test | 94% +| 84% (62/74) | 85% (11/13) | 100% (6/6) | | com.gandu.base.commons.validchecker | 90% +| 90% (9/10) | 50% (1/2) | 0% (0/0) | | com.gandu.base.commons.validchecker.arghandler | 12% +| 8% (115/1404) | 8% (8/99) | 0% (0/350) | | com.gandu.base.commons.validchecker.centralarea | 26% +| 37% (111/297) | 65% (17/26) | 56% (47/84) | | com.gandu.base.commons.validchecker.testtag | 85% +| 82% (613/748) | 82% (93/114) | 53% (90/170) | | com.gandu.base.consoleindependentcommons | 91% +| 91% (79/87) | 82% (14/17) | 90% (18/20) | | com.gandu.base.disk | 80% +| 79% (70/89) | 76% (16/21) | 75% (27/36) | | com.gandu.base.disk.test | 2% +| 3% (3/98) | 7% (1/14) | 0% (0/0) | | com.gandu.base.flow | 39% +| 37% (479/1281) | 41% (131/321) | 6% (21/336) | | com.gandu.base.flow.parser | 31% +| 34% (468/1363) | 34% (48/141) | 15% (51/348) | | com.gandu.base.helpcontent | 0% +| 0% (0/6) | 0% (0/4) | 0% (0/0) | | com.gandu.base.datafeeder | 0% +| 0% (0/144) | 0% (0/23) | 0% (0/66) | | com.gandu.base.datafeeder.test | 3% +| 4% (3/82) | 8% (1/12) | 0% (0/20) | | com.gandu.base.preferencefiles | 62% +| 55% (505/915) | 52% (15/29) | 21% (42/204) | | com.gandu.base.preferences | 50% +| 43% (1139/2620)| 28% (138/500) | 6% (38/688) | | com.gandu.base.report | 19% +| 22% (439/1954) | 61% (46/76) | 47% (105/224) | | com.gandu.base.report.emailreport | 91% +| 88% (244/278) | 93% (26/28) | 67% (28/42) | | com.gandu.base.report.test | 86% +| 86% (6/7) | 50% (1/2) | 0% (0/0) | | com.gandu.base.managerexec | 28% +| 26% (1101/4209)| 43% (90/208) | 34% (200/593) | | com.gandu.base.managerexec.runmanager | 18% +| 18% (344/1913) | 23% (46/200) | 19% (122/638) | | com.gandu.base.managerexec.rununitexecutor | 15% +| 18% (907/5149) | 40% (115/291) | 6% (61/1062) | | com.gandu.base.testmanager | 95% +| 89% (117/131) | 78% (21/27) | 82% (28/34) | | com.gandu.base.testmanager.housekeeping | 72% +| 69% (1044/1513)| 68% (116/171) | 50% (130/260) | | com.gandu.base.util | 10% +| 11% (48/450) | 13% (11/82) | 12% (13/107) | | com.gandu.base.util.test | 81% +| 80% (4/5) | 50% (1/2) | 0% (0/0) | ------------------------------------------------------------------ +-------------------------------------------------------

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11136164]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-01-21 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (57 votes). Check out past polls.

    Notices?