in reply to Re: Google has failed me! Using pack and substr for fixed width file output
in thread Google has failed me! Using pack and substr for fixed width file output

Hi Roboticus,

It's good to know that there are other fixed width hackers out there :-)

I did use pack for a while and I did find it easier and faster to create a solution using it. But I had a problem explaining how to match up the fields with the pack format to the supporting teams. They couldn't seem to get their heads around it. So, for me, I got called less if I developed the solution using substr coz (I suppose) it was easier for the support bods to conceptualise the approach.

Thanks though, it's nice to know I'm <drum roll please> part of the <ahem> pack. Thank you very much, I'm here til Thursday - try the fish.

Cheers, Jake

  • Comment on Re^2: Google has failed me! Using pack and substr for fixed width file output

Replies are listed 'Best First'.
Re^3: Google has failed me! Using pack and substr for fixed width file output
by AnomalousMonk (Archbishop) on Apr 09, 2014 at 09:13 UTC

    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 "