How can I line up columns with Perl's printf when the value might be a number or a string?
- by user317203
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