Whenever I am thinking about columns and fixed-length data files I think of pack and unpack. I not only find it easier to use but, generally a little faster than printf.
print MERGE pack('A20A180A200A350',@vars);

as for the faster part (I thought it would be a bigger difference):

#!/usr/local/bin/perl use Benchmark; use strict; my $count = 10000; my @lists = map { [chr($_+65),chr($_+66),chr($_+67),chr($_+68) ] } 0.. +22; open(FH1,'>/tmp/x') or die "$!\n"; open(FH2,'>/tmp/y') or die "$!\n"; timethese ($count, { 'pack' => '&use_pack', 'printf' => '&use_printf' +} ); sub use_pack { foreach (@lists) { print FH1 pack('A20A180A200A350',@{$_}); } } sub use_printf { foreach (@lists) { printf FH2 "%-20s%-180s%-200s%-350s", @{$_} ; } }
Benchmark: timing 10000 iterations of pack, printf... pack: 21 wallclock secs (10.10 usr + 9.55 sys = 19.65 CPU) @ 50 +8.91/s (n=10000) printf: 24 wallclock secs (10.90 usr + 9.72 sys = 20.62 CPU) @ 48 +4.97/s (n=10000) Benchmark: timing 10000 iterations of pack, printf... pack: 21 wallclock secs (10.03 usr + 9.75 sys = 19.78 CPU) @ 50 +5.56/s (n=10000) printf: 23 wallclock secs (11.88 usr + 10.72 sys = 22.60 CPU) @ 44 +2.48/s (n=10000)


grep
Mynd you, mønk bites Kan be pretti nasti...

In reply to Re: Formatting an output with printf by grep
in thread Formatting an output with printf by fiddler42

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.