Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Mis-aligned Data Output

by rgwest61 (Initiate)
on Jun 08, 2015 at 23:43 UTC ( [id://1129563]=perlquestion: print w/replies, xml ) Need Help??

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

Being new to Perl (converting my first Perl script that previously executed on Solaris server to Linux server) I have run into an issue where the data being displayed to main window does not line up under associated headers. Original code did not work and I attempted to use tab formats within sprintf with failing success. My application reads in multiple rows of data from Oracle database with various column types (text, floating, int, ...) for which the values for a specific column can be of different length for the rows being retrieved. I have read from other posts that the best option to correctly display such data is to use Perl Format. Before I go down the path to redesign and rewrite the script to use Perl Format I need to know that it supports the following in addition to displaying data headers and data nicely aligned: 1) Display of security classification header in addition to data headers 2) Selection of row(s) to perform processing on. Thanks for any/all assistance. 2)

Replies are listed 'Best First'.
Re: Mis-aligned Data Output
by eyepopslikeamosquito (Archbishop) on Jun 09, 2015 at 03:36 UTC

    I would not recommend using "Perl Format" for this. I think printf/sprintf is the way to go. To get you started, here is some simple example code:

    use strict; use warnings; my @vals = ( [ "hello there", 9, 7, 42, 3.14159 ], [ "another string", 12, 9, 123, 69.123 ], ); for my $r (@vals) { printf "%-20.20s %02d:%02d %5d %8.4f\n", $r->[0], $r->[1], $r->[2], $r->[3], $r->[4]; }

    Running this program produces:

    hello there 09:07 42 3.1416 another string 12:09 123 69.1230

    Please post the sprintf code you tried, explaining why it did not work and we should be able to help you.

      I was able to get the data display alignment problem resolved by implementing sprintf's zero padding on the left hand side of floating point displayed data such as %010.4f thereby forcing a consistent number of digits. Thanks for your time, recommendation and help.
Re: Mis-aligned Data Output
by 2teez (Vicar) on Jun 09, 2015 at 06:46 UTC

    In addition to what eyepopslikeamosquito already said and using the data presented, though the OP didn't show any. One can also look at these modules:

    • Perl6::Form
      use Perl6::Form; my @vals = ( [ "hello there", 9, 7, 42, 3.14159 ], [ "another string", 12, 9, 123, 69.123 ], ); print form "Words |Value1 |Value2 |Value3 |Value4"; for my $r (@vals) { print form "{<<<<<<<<} |{||||} |{||||} |{|||||} |{]]]]]} ", $r->[0], $r->[1], $r->[2], $r->[3], $r->[4]; }
    • Text::Table
      use Text::Table; my $tb = Text::Table->new( "Words", "Value1", "Value2", "Value3", "Value4 +" ); $tb->load(@vals); print $tb;

    You might have to look at the documentation of these modules.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found