in reply to Padding strings for a flat file
An approach I've found useful to construct records is to define a
template line consisting of spaces (and any data that doesn't
change from record to record), and then insert the fields using substr.
This way you don't need to worry about padding.
Thanks to Dominus for reminding me of the argument changing trick used by insert to change the record.#!perl -w use strict; my $template = ' ' x 60; sub insert { my (undef, $field, $position) = @_; substr($_[0], $position, length($field)) = $field; } my $record = $template; insert($record, 'abc', 0); insert($record, 'ghi', 6); print "[$record]\n";
|
---|
In Section
Seekers of Perl Wisdom