in reply to Assistance fixing a "format" call
Update: also with standard printf in Perl (or even C), you can dynamically generate the "format spec" - should that be necessary and its almost never necessary.#!/usr/bin/perl -w use strict; while (<DATA>) { my @tokens = split ' ', $_; printf "%-10s %-10s %-10s %-10s %-10s\n", @tokens; } =prints TECH SCANNER WEBSERVER TALKTOME TRAINING TESTING MOBILE TECHADMIN CIRCADMIN MANAGERS =cut __DATA__ TECH SCANNER WEBSERVER TALKTOME TRAINING TESTING MOBILE TECHADMIN CIRCADMIN MANAGERS
It is important to realize that with "format spec width" like %-10s specifies the minimum width. If the string is longer than that it will still be printed although the columns for that line won't line up. This almost always what you want - one goofy looking line in a 200 line report. The trailing space after the %-10s ensures that there will at least one blank space between columns and that is almost always what is desired. Extremely rigid, fixed field, "card punch image" formats are very rare nowadays. Adjust the column widths so that they work "almost all of the time" - when the outlier line comes along, humans are very good at integrating and seeing past that single line.while (<DATA>) { my @tokens = split ' ', $_; my $format = '%-10s ' x @tokens; printf "$format\n", @tokens; }
If you absolutely insist upon using a "format" despite my recommendation to the contrary, put the definition outside of the while() loop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Assistance fixing a "format" call
by bobdabuilda (Beadle) on Apr 02, 2012 at 03:18 UTC | |
by Marshall (Canon) on Apr 02, 2012 at 03:23 UTC |