Advertisement

05.12.2004 at 06:21AM PDT, ID: 20986669
[x]
Attachment Details

using printf or sprintf to format columns of data

Asked by texasreddog in Perl Programming Language

Tags: perl, printf, columns, format, using

I have an output file that is created with a Perl script that I want to align the data into readable columns.  The part of my script that does this is below:

printf OUT "%s          %s          %s          %s\n", $tag, $repo, $message, $developer;

I have 10 spaces in between each column of data, and the output looks like this:

Tag_CYY_20040511_1615          /home/cvs/eng/Sonata2/dev/clasi/include          Bugzilla #18291          chouston
SATURN_20040511_B_BRYAN          /home/cvs/eng/Sonata2/dev/clasi/include          Bugzilla #18291          chouston
SATURN_20040511_A_BRYAN          /home/cvs/eng/Sonata2/dev/clasi/include          Bugzilla #18291          chouston
SATURN_20040511_D          /home/cvs/eng/Sonata2/dev/clasi/include          Bugzilla #18291          chouston

I want to format the output so all the columns are aligned:

Tag_CYY_20040511_1615               /home/cvs/eng/Sonata2/dev/clasi/include          Bugzilla #18291          chouston
SATURN_20040511_B_BRYAN          /home/cvs/eng/Sonata2/dev/clasi/include          Bugzilla #18291          chouston
SATURN_20040511_A_BRYAN          /home/cvs/eng/Sonata2/dev/clasi/include          Bugzilla #18291          chouston
SATURN_20040511_D                     /home/cvs/eng/Sonata2/dev/clasi/include           Bugzilla #18291          chouston

I know you can do this with printf or sprintf, but what kind of syntax should I use?  And also, how can I guarantee that the columns will always be aligned, no matter how long each field of data might be?  Thanks!



Start Free Trial
 
Loading Advertisement...
 
[+][-]05.12.2004 at 06:31AM PDT, ID: 11049691

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: perl, printf, columns, format, using
Sign Up Now!
Solution Provided By: stefan73
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.12.2004 at 07:54AM PDT, ID: 11050623

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.12.2004 at 08:43AM PDT, ID: 11051190

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32