Solved

printf to format columns

Posted on 2004-09-07
4
372 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
  • 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now