in reply to Re^2: Help with Creating a Table from array ref I parsed in
in thread Help with Creating a Table from array ref I parsed in

Hi TIMTOWTDI,

I am a little lost trying to figure out how your script works.

I read Perl #sprintf FORMAT, LIST and could not understand how your "format STDOUT" works. Can you explain it? The Perldocs I read do not have the format you presented here for your script

Say, if I were to "left-justify" each column, how can I add the "-" to your "format STDOUT" statement and be left-justified? Also, how to manipulate the spacing in between columns?

I's like the table to look like this below, perhaps, I can understand your format statement works if I see it...maybe?

MM_STD_gate 152.401574 -22.047244 mils 3871.000000 -560.000000 microns RFN_a 337.244094 0.787402 mils 8566.000000 20.000000 microns RFN_b 366.181102 0.787402 mils 9301.000000 20.000000 microns RFN_c 376.929133 0.787402 mils 9574.000000 20.000000 microns RFLR 366.181102 -10.6299921 mils 9301.000000 -270.000000 microns MM_STD2_gate 259.488188 -22.047244 mils 6591.000000 -560.000000 microns RBASE_right_2 -366.181102 -33.464566 mils -9301.000000 -850.000000 microns

This is your code below with the edit to allow a longer string for the 1st column

#!/usr/bin/env perl use strict; use warnings; my @fields; # For the longer 1st Column name, I added some more spaces so it won't + concat the name. NOt sure how the left-justify can be done with this + format STDOUT statement? format STDOUT = @<<<<<<<<<<<< -@#####.###### @####.###### @<<<<<< @fields . while (my $line = <DATA>) { chomp $line; @fields = split /\s+/, $line, 4; unshift @fields, ' ' if 4 > @fields; write; } # All four columns should be left-justified and should line up whether + it is a positive or negative number. __DATA__ MM_STD_gate -61.771653 -45.472441 mils -1569.000000 -1155.000000 microns RFN_a -91.102362 68.307087 mils + -2314.000000 1735.000000 microns RFN_b -62.165354 68.307087 mils + -1579.000000 1735.000000 microns RFN_c -51.417322 68.307087 mils + -1306.000000 1735.000000 + microns MM_STD2_gate 259.488188 -22.047244 mils 6591.000000 -560.000000 microns RBASE_right_2 366.181102 -33.464566 mils 9301.000000 -850.000000 microns

Replies are listed 'Best First'.
Re^4: Help with Creating a Table from array ref I parsed in
by hippo (Archbishop) on May 16, 2022 at 08:40 UTC
    Hi TIMTOWTDI

    Hi perlynewby. TIMTOWTDI is an abbreviation: in this context it was there to highlight that the method I showed was different to that from kcott. I'm hippo.

    I read Perl #sprintf FORMAT, LIST and could not understand how your "format STDOUT" works. Can you explain it? The Perldocs I read do not have the format you presented here for your script

    You have been reading different docs. Please see the docs for format which I linked to previously and which are entirely unrelated to sprintf. Hopefully they will make it all clear. Give it a try once you have read through those and if you are still stuck please ask further.


    🦛

Re^4: Help with Creating a Table from array ref I parsed in
by dbuckhal (Chaplain) on May 16, 2022 at 04:57 UTC

    Here is an earlier reply I did using format with aligned columns using sprintf: https://www.perlmonks.org/?node_id=11136290

    sample output from that thread:
    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) | ------------------------------------------------------------------ +-------------------------------------------------------
    Fun stuff!