When I first saw this format thing, I thought oh, how cool. After some experience with it, I have changed my mind! The old standby printf just works out better in practice, at least for me. My advice would be to get rid of that "format" stuff.
#!/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
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.
while (<DATA>) { my @tokens = split ' ', $_; my $format = '%-10s ' x @tokens; printf "$format\n", @tokens; }
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.

If you absolutely insist upon using a "format" despite my recommendation to the contrary, put the definition outside of the while() loop.


In reply to Re: Assistance fixing a "format" call by Marshall
in thread Assistance fixing a "format" call by bobdabuilda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.