?
Solved

printf to format columns

Posted on 2004-09-07
4
Medium Priority
?
377 Views
Last Modified: 2008-02-01
Hello All!

I am new to Perl, so please forgive my limited knowledge...

I am reading in a CSV file and outputting the contents to several proprietary data formats that we use in-house.  In order to do this, I need to reformat some of my data to fit within a 5 character column width.

I need to format as in the following example (note - column width is 5 characters):
1 -> 1.000
1.25 -> 1.250
23.2 -> 23.20

According to my reading, I should probably use something like printf("%0-5s","1.25") in order to format 1.25 to 1.250 however this isn't working for me!?!?

Any ideas???

Thanks!
0
Comment
Question by:AirResourceSpec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 4

Expert Comment

by:divt
ID: 11999714
Here a rough sample.

CODE:

$n[0]=0.1;
$n[1]=0.12;
$n[2]=2.3;
$n[3]=45.67;
$n[4]=980;
$n[5]=123.456;
$n[6]=1234.567;
$n[7]=12345.678;

foreach $current_n (@n) {
    $formatted_n = substr(sprintf("%5.3f", $current_n), 0, 5);
    print "$formatted_n \n";
}

OUTPUT:

0.100
0.120
2.300
45.67
980.0
123.4
1234.
12345


Notice the problem when you have 4-digit integer.  Would you accept "1234." or rather "1234" ?
0
 
LVL 4

Accepted Solution

by:
divt earned 1000 total points
ID: 11999745
Just thought I should include your sample numbers:

CODE:

$n[0]=1;
$n[1]=1.25;
$n[2]=23.2;
$n[3]=0.1;
$n[4]=980;
$n[5]=1234;  
$n[6]=12345;  
$n[7]=12345.678;

foreach $current_n (@n) {
    $formatted_n = substr(sprintf("%5.3f", $current_n), 0, 5);
    print "$formatted_n \n";
}


OUTPUT:

1.000
1.250
23.20
0.100
980.0
1234.
12345
12345
0
 
LVL 1

Author Comment

by:AirResourceSpec
ID: 12000253
So far, so good... I only have time to do a bit of testing today, but so far this seems to be working great!  Thanks!!!
0
 
LVL 4

Expert Comment

by:divt
ID: 12004694
Glad to hear so.  :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question