How can I line up columns with Perl's printf when the value might be a number or a string?

Posted by user317203 on Stack Overflow See other posts from Stack Overflow or by user317203
Published on 2010-04-15T06:46:27Z Indexed on 2010/04/15 9:03 UTC
Read the original article Hit count: 296

Filed under:
|

I am trying to output a document that looks like this (more at http://pastebin.com/dpBAY8Sb):

     10.1.1.1       100   <unknown>              <unknown>              <unknown>         <unknown>         <unknown>
72.12.148.186        94   Canada                 Hamilton               ON                43.250000        -79.833300   0.00
72.68.209.149        24   United States          Richmond Hill          NY                40.700500        -73.834500   611.32
 72.192.33.34         4   United States          Rocky Hill             CT                41.657800        -72.662700   657.48

I cannot find how to format the output I have to have a floating poing and format the distance between columns. My current code looks something like this.

if (defined $longitude){
   printf FILE ("%-8s %.6f","",$longitude);
}else{
 $longitude = "<unknown>";
   printf FILE ("%-20s ",$longitude);
} 

The extra "" throws off the whole column and it looks like this (more at http://pastebin.com/kcwHyNwb).

10.1.1.1             100       <unknown>           <unknown>            <unknown>            <unknown>          <unknown>                 
72.12.148.186        94        Canada              Hamilton             ON                     43.250000         -79.833300           0.00
72.68.209.149        24        United States       Richmond Hill        NY                     40.700500         -73.834500           571.06

© Stack Overflow or respective owner

Related posts about perl

Related posts about printf