There are all kinds of interesting games you can play with pack, and it should be easy to encapsulate your solution in a function (with lotsa validation) so that maintainers only see data. See below.

Note that the  @ pack template specifier fills with nulls, so if you want space-filling, you need the
    $packed =~ tr{\000}{ };
statement. Note also that  @ is absolute, so maybe play around and see, e.g., what happens if the  date offset is set to 1 or 2.

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use constant ORDER => qw(date client_id info); ;; my %o_l = ( date => { qw(off 0 len 6 field 040814) }, client_id => { qw(off 6 len 8 field WayTooMuchClientID) }, info => { qw(off 20 len 7 field G1APD) }, ); ;; my $fields = join ' ', map qq{\@$o_l{$_}{off} A$o_l{$_}{len}}, map { exists $o_l{$_} or die qq{bad '$_'}; $_; } ORDER ; ;; my $packed = pack $fields, map $o_l{$_}{field}, ORDER; $packed =~ tr{\000}{ }; print 'packed len ', length $packed, qq{ '$packed'}; ;; my $total = 30; die 'truncation!' if length($packed) > $total; my $line = pack qq{A$total}, $packed; print 'total len ', length($line), qq{ '$line'}; ;; dd \$line; " packed len 27 '040814WayTooMu G1APD ' total len 30 '040814WayTooMu G1APD ' \"040814WayTooMu G1APD "

In reply to Re^3: Google has failed me! Using pack and substr for fixed width file output by AnomalousMonk
in thread Google has failed me! Using pack and substr for fixed width file output by spudulike

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.