in reply to Re^2: Best way(s) to process form data into fixed-length values?
in thread Best way(s) to process form data into fixed-length values?

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)

Replies are listed 'Best First'.
Re^4: Best way(s) to process form data into fixed-length values?
by GrandFather (Saint) on Jul 26, 2006 at 04:16 UTC

    Note that %5s will not truncate a string that is too wide. You need %5.5s to do that.


    DWIM is Perl's answer to Gödel