Why would you go to the trouble of pre-padding them? I'm pretty sure you'd be better off performance-wise to just call sprintf once (if that's any sort of concern).

Assuming an example where odd fields are 5 chars long and evens 15:

my $var1, $var2, $var3, $var4, $var5; #hopefully you'll be using some +meaningful names instead my $format = "%5s%15s%5s%15s%5s\n"; #you could embed spaces in the for +mat specifier too if needed #check/process inputs here #.... $outputline = sprintf $format, $var1, $var2, $var3, $var4, $var5;
See the sprintf documentation (pg 797 of Programming Perl 3rd e) for other formats if you need to deal with formatting numerics.

You can break the format statement up or use repeat specifications too so you don't lose count:

my $format = "%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s". "%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s". "%5s%5s%5s%5s%5s"."%5s%5s%5s%5s%5s"; my $otherformat" = "%5s"x40;
(Though the former is likely to be unwieldy when the client changes the content three times a day)

In reply to Re^3: Best way(s) to process form data into fixed-length values? by Anonymous Monk
in thread Best way(s) to process form data into fixed-length values? by hmbscully

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.