Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

printf to format columns

Posted on 2004-09-07
4
Medium Priority
?
380 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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

810 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